<?xml version="1.0" encoding="windows-1252"?>
<node id="862038" title="A matter of style: how to perform a simple action based on a simple condition?" created="2010-09-25 23:26:43" updated="2010-09-25 23:26:43">
<type id="115">
perlquestion</type>
<author id="333489">
muba</author>
<data>
<field name="doctext">
&lt;p&gt;After having written a couple of programs in a not very well known programming language that usually employs a style of &lt;c&gt;some_variable &amp;&amp; some_subroutine()&lt;/c&gt; to run a subroutine only when some_variable holds a true value, I find myself doing the same in Perl often lately.&lt;/p&gt;

&lt;p&gt;But then I realised that Perl also has the &lt;c&gt;some_subroutine if $some_variable&lt;/c&gt; syntax, and of course the filled with line noise version, &lt;c&gt;if ($some_variable) { some_subroutine }&lt;/c&gt;.&lt;/p&gt;

&lt;p&gt;All of these have pros and cons, I think.&lt;/p&gt;

&lt;readmore&gt;
&lt;p&gt;&lt;b&gt;One&lt;/b&gt;&lt;br&gt;&lt;c&gt;$var &amp;&amp; subroutine&lt;/c&gt;: 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 &lt;i&gt;two&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Two&lt;/b&gt;&lt;br&gt;&lt;c&gt;subroutine if $var&lt;/c&gt;: 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.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Three&lt;/b&gt;&lt;br&gt;&lt;c&gt;if ($var) { subroutine }&lt;/c&gt;: has the advantage of snippet &lt;i&gt;one&lt;/i&gt; (condition first, consequence next), but is filled with line noise. &lt;c&gt;( ) { }&lt;/c&gt; just for a really simple 'IF this THEN that' kind of construction.&lt;/p&gt;

&lt;/readmore&gt;

&lt;p&gt;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.&lt;/p&gt;</field>
</data>
</node>
