To die or to croak, that is the question

Lately I've been tempted to use croak() instead of die(). Somehow it seems more considerate to users. But finally in the end I'm sticking with die(). In fact, I think the Carp module should be, well, croaked.

The reasons:

1. Even though Carp has been included in Perl 5 since forever (Module::CoreList tells me: "5"), carp(), croak(), cluck(), and confess() are still not builtins, which means I still need an extra "use Carp".

2. Too many keywords! Most other languages only have "throw" or "raise".

3. Names are too weird! I understand the difficulty of coming up with a concise set of names that are similar but slightly different. But requiring these weird names might also indicate that there is something fishy about the concept itself.

4. The choice of showing a stack trace or not should not be in the individual functions. That burdens the programmer with too much thinking.

5. Even with Carp qw(verbose), what's to be done with codes that still die() and warn()? (But luckily there's Carp::Always.)

6. Showing stack trace should not be this difficult. I still think there should be a command-line switch for Carp::Always (or alias it to 'oan' :-)

7. Programmers (module writers) make mistake. They should not skip a call frame.

In short, I think Carp makes things a little bit too complicated. But what's Perl without complication? :-)

5 Comments

You know, I've been programming for a long time, and I don't think I've ever found a stack trace particularly helpful. You ever try to work your way through one of those monster Moose error messages? It's like swimming through Jello.

That said, I do find myself using `carp` and `croak` (but never the other ones) very frequently. Any time I find myself writing a `warn` or `die`, I ask if this warning or error is my fault, or the fault of the guy using my module. If the latter, I `carp` or `croak` instead.

Hi Folks

I've reverted from croak to die, since then I can capture consistently with Capture::Tiny.

To be fair though, a Java stack trace is just sick compared to the others.

Interestingly, vim's default perl syntax highlighting will highlight croak as if it was a built-in function. http://p3rl.org/Devel::SimpleTrace is a nice module to consider.

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays). My CPAN ID: SHARYANTO. I'm sedusedan on perlmonks. My twitter is stevenharyanto (but I don't tweet much). Follow me on github: sharyanto.