<?xml version="1.0" encoding="windows-1252"?>
<node id="1013951" title="Re: Two simple code style advice questions (tye)" created="2013-01-17 23:56:06" updated="2013-01-17 23:56:06">
<type id="11">
note</type>
<author id="22609">
tye</author>
<data>
<field name="doctext">
&lt;p&gt;
I'd use 1b, in part because it is an important idiom.  On a single line is even better.  1a is okay (the one advantage I'll grant it is that it avoids repeating either variable name -- I'm not sure why I've long used this only grudgingly).  But if I wanted to go for maximum clarity, IMHO, I'd instead do:
&lt;/p&gt;&lt;c&gt;
my %ntests;  $ntests{$_} = 0 for @tests;
&lt;/c&gt;&lt;p&gt;
Then there is:
&lt;/p&gt;&lt;c&gt;
@{ \my %ntests }{ @tests } = (0)x@tests; # :)
&lt;/c&gt;&lt;p&gt;
2a is pretty darn hackish and not something I'd expect to see in professional code.  But I've come to find ternaries to often be less quick/easy to read than something like:
&lt;/p&gt;&lt;c&gt;
my $mol = '';
$mol = 'forty two'
    if  42 == $n;
&lt;/c&gt;&lt;p&gt;
Despite being 3 lines instead of 1, I find it significantly easier and faster to parse the intent.  I especially don't like how the default value is almost lost in the single-line ternary.  But I'd probably feel I was being a bit extravagant and then just use the 1-line ternary if the names and values are actually that short.  For more and/or longer expressions, I'd format it more like:
&lt;/p&gt;&lt;c&gt;
my $mol =
    ! defined $n
        ? 'n/a' :
    42 == $n
        ? 'forty two'
        : '';
&lt;/c&gt;&lt;p&gt;
(If I didn't use multiple "assignment \n\t if ...;" statements.)
&lt;/p&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-22609"&gt;&lt;p align="right"&gt;
- [tye]&lt;tt&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/tt&gt;
&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1013548</field>
<field name="parent_node">
1013548</field>
</data>
</node>
