in reply to One out of three ain't bad

I'm not sure what solution to offer. Don't shoot for concise, short for maintainablity. Short circuiting should be avoided in perl, it has better faculties.

## Instead of...
$x && $count++;
## Consider...
$count++ if $x;

I would probably go with the slightly modified grep suggested in the post above. But see 'perldoc -q contain in'


Evan Carroll
www.EvanCarroll.com

Replies are listed 'Best First'.
Re^2: One out of three ain't bad
by davido (Cardinal) on Oct 22, 2005 at 06:28 UTC

    Short circuiting should be avoided in perl, it has better faculties.

    Tell that to the POD. The docs for open, for example, give at least a dozen examples of constructs similar to this:

    open FH, '>', 'filename' or die $!;

    ...and who could argue that use of short circuiting is inferior to this:

    die $! unless open FH, '>', 'filename';

    This is further discussed in the following quote from perlstyle:

    Here are some other more substantive style issues to think about:

    • Just because you CAN do something a particular way doesn't mean that you SHOULD do it that way. Perl is designed to give you several ways to do anything, so consider picking the most readable one. For instance

      open(FOO,$foo) || die "Can't open $foo: $!";

      is better than

      die "Can't open $foo: $!" unless open(FOO,$foo);

      because the second way hides the main point of the statement in a modifier.

    The point here is that you cannot make a sweeping statement such as "short circuiting should be avoided in Perl". Avoided why? There are several equally efficient ways to do things. The docs are right: "consider picking the most readable one." You might be right that short circuiting hides the intent in this case, but certanly it's going too far to say it should be avoided altogether.


    Dave