Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Conditional style (if, &&) query

by George_Sherston (Vicar)
on Oct 17, 2001 at 03:00 UTC ( [id://119282]=note: print w/replies, xml ) Need Help??


in reply to Conditional style (if, &&) query

I shouldn't like anyone to think I'm an expert, but my £/50 is that it's nice to make the code read as much like English as possible. When I'm writing prose, I try to put the most important bit of the sentence in the most prominent place. So, say there are two buttons, a read and a blue one, and the red one is the ejector seat. If I'm writing to somebody who knows that one of the buttons will blow you out of your seat, I'd write
if you press the red button, the ejector seat will launch
But if I was writing to somebody who knew the red button was a bad idea but didn't know exactly what was bad about it, I might write
the ejector seat will launch if you press the red button
So my choice between B and C is
if (the condition is more significant than what I do if the condition +is met) { use option C; } elsif (what I do if the condition is met is the interesting bit) { use option B; } else { probably use C on the same basis as [bjohnso]; }
You notice that here I used C, since we all know what the different options are, and what's at issue is under what conditions we use them.

§ George Sherston

Replies are listed 'Best First'.
Re: Re: Conditional style (if, &&) query
by jackdied (Monk) on Oct 17, 2001 at 03:12 UTC
    This comment is dead on, to extend the metaphor

    # check for a pilot action if ($red_button_pressed) { eject_seat(); } eject_seat() if $massive_failure; # check for exceptional behavior # sub returns true/false if the seat was ejected return $red_button_pressed && $massive_failure && 1; # fixed to put th +e && 1 on the end, in the original it was at the start
    The '1' is a perl-ism. Perl will return the value of the last true value in the boolean. To prevent people from taking shortcuts and expecting the sub to return false or the value of $red_button_pressed or $massive_failure we return one. Sooner or later it will no longer return what they think it does, and their code will melt (see also bitrot).

    -jackdied

      Your last bit of code confused me....
      return 1 && $a && $b;
      is identical to
      return $a && $b;
      is identical to
      return 1 && 1 && 1 && $a && $b
      So I don't follow your last paragraph...

      Update
      Were you perhaps thinking of:

      return $a && $b && 1;
      which will "coerce" all TRUE responses to be 1 instead of the value of $b?

      -Blake

Re: Re: Conditional style (if, &&) query
by Elliott (Pilgrim) on Oct 17, 2001 at 12:53 UTC
    I have to admit that after 30 years of seeing Basic and Cobol programs structured
    if (cond) do something else do something else
    It may be prejudice that makes me prefer this structure. But I do think there is good reason too.

    George_Sherston's comment about the relative importance of the things_to_do is a good one and I take this into account by making free use of unless() {}, but there's another issue here, for which I will put on my tester's hat. It is important that we see clearly that it is a condition. From the perspective of a test designer, the presence of a condition is more important that what it does. And since I think better code gets written if everyone who comes into contact with it thinks about it from a tester's perspective, that makes "C" style a clear winner for me.

    BTW SInce I was using the "C" style a long time before C was invented, I find it hard to accept the phrase "traditional C style"!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://119282]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-03-29 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found