The best solution is the one that's the most readable and the easiest for other people to understand. That might not always be the same way.

By taking out all the complexity, you take away most of the information we'd need to judge a solution.

The way that you write these things tells other programmers a bit about what you were thinking. Code exists for one human to tell another human something. Although the humans might interpret your intent incorrectly (you can argue elsewhere who's dumber: the compiler or the person), here's what I see in your code:

The first style says that the first branch will probably execute more frequently, but either branch is equally expected (more or less). Compare that below with what I see in the last example.

sub sg { if (COND) { return 1; } else { return 2; } }

Your second example says that you're very cautious because you don't trust what you see. You should never reach that die statement, but you think that you might so you put it there to catch bugs. You probably don't care out coverage testing too much when you purposedly add unreachable code.

sub sg { if (COND) { return 1; } else { return 2; } die "should never reach this"; }

The last one says that normally this subroutine returns a certain value (computed, from a variable, whatever), but in some special cases you want to short-circuit all the processing and return a different value. Most of the time, however, you expect the full subroutine to execute.

sub sg { if (COND) { return 1; } return 2; }
--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

In reply to Re: if else and not reachable coding style by brian_d_foy
in thread if else and not reachable coding style by szabgab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.