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

Matt Tucker tuck at whistlingfish.net
Fri Mar 15 02:43:50 CST 2002

-- 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.

For instance, in the case of:

    if ($a == 'foo' or $b == 'bar') {

it seems fairly clear to me, without the clutter of extra parentheses,
what's intended here. In the case of:

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

I also believe the intent is clear. Of course, if precendence doesn't
support the intended results or if the intent is potentially unclear,
then by all means throw them in.

This is, of course, my style; I certainly wouldn't ask anyone else to
follow it. And as for brain cells, I figure I can probably spare the
few required to do this.  ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
Url : http://mail.pm.org/archives/spug-list/attachments/20020315/3d7f0db7/attachment.bin

More information about the spug-list mailing list