in reply to Re (tilly) 4: Add hash to DB_HASH file using DB_File
in thread Add hash to DB_HASH file using DB_File

The danger of adopting an "oh, another set of parenthesis won't hurt" attitude with Perl is that you'll nail yourself by introducing a list (or list context) when you don't intend to.

Worse, people who aren't at your level of skill will carry on the practice through "Cargo cult" programming, and will trip all over themselves.

  • Comment on Re: Re (tilly) 4: Add hash to DB_HASH file using DB_File

Replies are listed 'Best First'.
Re (tilly) 6: Add hash to DB_HASH file using DB_File
by tilly (Archbishop) on Apr 09, 2001 at 21:52 UTC
    I firmly believe that the number of accidental mistakes through changing context is far outweighed by the mistakes which are prevented through misunderstanding relative precedence. As for cargo cult programming, yeah. Anything you do can be picked up as a meme and propagated blindly. But still considering the relative frequency of errors made/prevented, I like adding the parentheses. If people are going to blindly follow habits, let them at least follow good ones.

    And I consider parenthesizing for clarity to be a good practice. (Note that had kha0z followed that practice with the tie we would not be having this discussion right now...)

    FWIW I am in good company with my opinion, in perlstyle adding unnecessary parentheses is listed as a "more substantive style issue". The relevant section says:

    Along the same lines, just because you CAN omit parentheses in many places doesn't mean that you ought to: return print reverse sort num values %array; return print(reverse(sort num (values(%array)))); When in doubt, parenthesize. At the very least it will let some poor schmuck bounce on the % key in vi. Even if you aren't in doubt, consider the mental welfare of the person who has to maintain the code after you, and who will probably put parentheses in the wrong place.
    So yes. It is possible to put parentheses in the wrong place and accidentally ruin your context. That is a mistake I only rarely see and I have never made. But I have both seen and made plenty of mistakes where parenthesizing would have prevented a mistake.

    In fact the only common mistake with parentheses that I know of is being tripped up by the fact that print is a function. I consider learning that fact to be far less to ask than any significant part of the precedence table. Particularly since a messed-up print tends to be far more obvious mistake than a non-functional error check. (I try to test my error checks, but I know full well that most people do not...)

    UPDATE
    I agree that it is situational. If you come from C then the precedence is pretty much trivial because the table is (intentionally) the same. For people who are not from a C background or whose C is rusty... (And yes, I have seen similar code from people used to C trying out Perl.)

      Point taken. Perhaps it's situational. I see far more problems with context confusion than I do mistakes with operator precedence, but that may be because I'm working with a set of people who've come up through C/C++.

      Their C/C++ backgrounds shows itself by way of things like

      if ( ($x > 0 ) && ( $y < 0 ) ) { return ( 1 ); }