Rabu, 28 April 2010

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? :-)

3 komentar:

  1. Croak and die are not the same. You didn't mention at all the differences between croak and die. Choosing the wrong one makes tracing errors more difficult.

    Carp has its own purpose above and beyond die. Carp is useful for module authors to report errors.

    Carp's subs are used for reporting incorrect module usage like bad parameters to subs. If any internal error occurs, I use die or confess so the line number can help me trace the error. I use these a bit like assert() in C.

    If you do not understand Carp's purpose it's not surprising you don't like it. You didn't mention Carp's differences from die so I though I'd stand up for it. Don't give up on it!

    BalasHapus
  2. Komentar ini telah dihapus oleh penulis.

    BalasHapus
  3. @Juster: Thanks for the comment. Since this is a Perl blog, I assume readers know the difference between die() and carp(). I never said they are the same. Why would I compare the two and choose/advise one against the other if I think they are the same? :-)

    Your comment illustrates exactly one of the problems with Carp:

    1) "Choosing the wrong one makes tracing errors more difficult."

    IMO, module authors should *not* choose at all. They should just die() or warn(). Tracing can be enabled in debugging sessions via a runtime switch.

    2) "If any internal error occurs, I use die or confess so the line number can help me trace the error."

    So you would open your module in an editor and change "croak" to "confess", just to trace internal errors? That is at best an overkill and at worst wrong. Yet by providing separate carp/cluck and croak/confess, this is the sort of thing that Carp seems to encourage, even though it also has Carp qw(verbose) aka $Carp::Verbose.

    Also as other has pointed out, croak() doesn't play nicely with Capture::Tiny.

    In short, I'm advocating plain die/warn. When you need stack trace, use -MCarp::Always in your Perl switch. No need to decide to use croak/confess or carp/cluck, too much thinking.

    BalasHapus