SPUG: RE: Precedence and parentheses [was: How to use database ...]

dancerboy dancerboy at strangelight.com
Fri Mar 15 20:55:37 CST 2002


At 12:43 AM -0800 3/15/02, Matt Tucker wrote:
>-- dancerboy <dancerboy at strangelight.com> spake thusly:
>
>>  At 12:10 PM -0800 3/14/02, Matt Tucker wrote:
>>>
>>>      my $email = "$row[12]";
>>
>>  I agree that this is bad style.  However, it is not *strictly* true
>>  that "you'll get exactly the same thing without them"
>
>Good point. Thanks for the clarification.
>
>  >      my $email = ( defined($row[12]) ? $row[12] : '' );
>>
>>  (And yes, I know that the parentheses in the above statement are
>>  unnecessary.  But personally, one of my biggest pet peeves is
>>  developers who won't use extra parentheses to clarify their code.  I
>>  consider the following to be atrocious style:
>>
>  >      my $email = defined $row[12] ? $row[12] : '';
>>
>>  IMO, you should always, in all but the most trivial and obvious of
>>  cases, use parentheses to make the order of operations explicit. Real
>>  developers have better things to do with their brain-cells than
>>  memorize operator precedence.)
>
>While I certainly understand this viewpoint, my philosophy here is a
>bit different. I prefer to not use parentheses whenever possible
>because I feel it makes the code look cleaner and more readable. I
>figure that as long as the code does what it _looks_like_ it's supposed
>to do, parentheses are unnecessary.

Well, this example was probably a poor one, since a line like

     my $email = defined $row[12] ? $row[12] : '';

arguably qualifies as one of those "most trivial and obvious of cases".

OTOH, when considering coding style, do remember this:  often, when a 
developer is looking at someone else's code, it's because something 
isn't working the way it's supposed to.  They're looking for bugs. 
When I see an expression like the one above, it may be obvious what 
the original developer *intended* -- but, depending on the context, I 
may still have a reasonable doubt about whether that statement is 
actually doing what the developer intended.  Unless I have Perl's 
operator-precedence tables memorized, a line like this becomes one 
more *potential* coding error that I have to investigate.  Does the 
above really do what it looks like it's doing? or is it perhaps being 
parsed like this:

     $email = defined( $row[12] ? $row[12] : '' );

?  or perhaps even like:

     ( $email = defined $row[12] ) ? $row[12] : '';

?

But if the original developer spells it out as

     $email = ( defined($row[12]) ? $row[12] : '' );

Then I know, without having to consult an operator-precedence table, 
that this line is at least doing what it looks like it's doing.  It's 
one less thing that I have to worry about.

Admittedly, I may have a somewhat biased view, since much of my work 
lately has involved sifting through reams of poorly-written, 
undocumented code created by developers who didn't know what the 
${expletive} they were doing, and trying to figure out which of the 
tortuous, illegible constructions are actually coding errors, and 
which just look like it.  :-/

And I agree that deeply-nested parens can easily make the code 
difficult to read.  That's why I personally advocate indenting 
sub-expressions and aligning parens the same way you would 
curly-braces.  E.g. in my own code I would actually write the above 
line as:

     $email = (
         defined( $row[12] )
         ?   $row[12]
         :   ''
     );

Someday I think I'm going to put together a "Jason Lamport Coding 
Style" style-guide (not in the spirit of "this is the best way to 
code and everyone should do it my way!" but more as a set of 
conventions that I personally have found helpful, and that others 
might find helpful as well)...

-jason

P.S.  Why was this thread labeled "[OT]"?  Seems to me discussions of 
coding style are perfectly on-topic for a Perl discussion list?

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org





More information about the spug-list mailing list