in reply to No braces

I really prefer the C/PHP way:

if(COND) do_something();

Because that's how I logically think. In perl, it's counter-intuitive to say something like:

do_something() if(COND);

The test happens before it ever decides whether to do_something(), therefore, the test should come first in the code. Oh well.

Replies are listed 'Best First'.
Re: No braces
by Abigail-II (Bishop) on Feb 11, 2004 at 12:42 UTC
    In perl, it's counter-intuitive to say something like:
    do_something() if(COND);
    Why is it counter-intuitive in Perl? It might be counter-intuitive for you, but that doesn't mean you can generalize that. It may be counter-intuitive for you to say I use my umbrella if it rains, but that doesn't make it counter-intuitive to say it in English. For the majority of the English speakers, it's not counter-intuitive to say "Effect if condition", and the counter intuitiveness of such a statement doesn't depend on the language.

    Abigail

      I didn't mean that "just in perl" it's counter-intuitive, I meant, the way perl forces you to do it is counter-intuitive. Of course what is intuitive differs depending on the person, I just find it much more natural to put the condition first. I would never say, "I use my umbrella if it rains." I would say, "If it rains, I'll use my umbrella." Of course as the reply stated below, I could wrap the whole thing in parens and use && but that (IMO of course) is still not as intuitive as saying if(COND) do_something(); I've been working with perl now for years and I still pause and scratch my head when I see that in code. I fear I'll never get used to it.

      Either way, I didn't mean to say that it was only counter-intuitive in perl.
        I meant, the way perl forces you to do it is counter-intuitive.
        But Perl doesn't require you to do it this way. Perl allows to put either the cause, or the effect to go first.

        Abigail

Re: Re: No braces
by flyingmoose (Priest) on Feb 11, 2004 at 15:10 UTC

    A common usage is when you don't want the if statement to obscure the readability of the code:

    print "DEBUG: foo is $x\n" if $config->{debug};

    is less distracting than:

    if ($config->{debug}) { print "foo is $x\n"; }

    The above statement reads more like English. Also, you can have main-path code not be obscured by error-handling logic, similarly enhancing readability, but this time in the reverse kind of way:

    reticulate_splines() unless llamas_roaming_in_strawberries()

    In the above example, we usually reticulate the splines (say 90% of the time), but in emergencies, we skip the statement.

    If all languages read exactly like C, it would be boring.

Re: Re: No braces
by ff (Hermit) on Feb 11, 2004 at 14:44 UTC
    For me, the irritating part about reading code with "if" qualifiers at the end is the sense of letdown I get from figuring out the first part of the line only to see that it might not fire because of the "if". However,

    do_something() if COND;

    buys me both that initial visual cue that the statement is qualified as well as the satisfaction of the simpler, less "noisy", flavor of "if".

      Which part is more important? The conditional or the statement? In some cases its one, some cases the other. And if you really want the conditional in front then just write:

      ($CONDITION) && do_something();

      But usually you are more interested in what might happen than exactly why so that becomes

      do_something() unless $exception;

      But sometime the conditional part really aught to go first:

      open ... or die ....

      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi