While I'm a believer in defensive programming in many ways, I can't help but wonder if in this particular case, the cure is worse than the disease. The subroutine's caller is presumably expecting a valid response ($this or $that, say), yet the code has a fallback case where the routine simply returns
something... who knows what. This strikes me as a potential debugging nightmare, because if something gets fouled up in the sub, the program might not break right away -- the sub would just return some (probably bogus) data, which could keep getting processed until it causes some bug way later in the program. Such bugs are, in my experience, extraordinarily hard to track down, because it's hard to know where they even originated.
If I were worried about the possibility that I might forget to explicitly return a value from within one of the branches of my if statement, I'd probably code the sub more like this:
sub blah
{
if($somevar == $somevalue)
{
return $this;
}
else
{
return $that;
}
die "Something very bad is happening";
}
which is to say, I would try to scream about the problem as early in the logic chain as possible, rather than sweep it under the rug where someone else might trip over it later.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.