After having written a couple of programs in a not very well known programming language that usually employs a style of some_variable && some_subroutine() to run a subroutine only when some_variable holds a true value, I find myself doing the same in Perl often lately.
But then I realised that Perl also has the some_subroutine if $some_variable syntax, and of course the filled with line noise version, if ($some_variable) { some_subroutine }.
All of these have pros and cons, I think.
One $var && subroutine: this one follows the way you formulate logic propositions: it is immediately apparent that subroutine() is only called when $var is true. However, readability might suffer a bit as it appears a bit "cryptic", especially compared to snippet two
Two subroutine if $var: flows like natural language, woohoo! However, the fact that subroutine() is called when $var is true is only revealed at the end of the statement.
Three if ($var) { subroutine }: has the advantage of snippet one (condition first, consequence next), but is filled with line noise. ( ) { } just for a really simple 'IF this THEN that' kind of construction.
Obviously, the TIMTOWTDI principle applies here but what I've come to ask is which of these styles do you prefer, and why? Just out of curiousity.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|