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

Re: A matter of style: how to perform a simple action based on a simple condition?

by Your Mother (Archbishop)
on Sep 26, 2010 at 17:36 UTC ( #862099=note: print w/replies, xml ) Need Help??

in reply to A matter of style: how to perform a simple action based on a simple condition?

What GrandFather said. I use Perl because it's not all those other construct limited languages that force you to reason like a 50s robot. The length and complexity of the code will change what is easiest to read but in general, this is what I'd say for all simple clauses, so this is what I understand the most easily–

Go shopping later if you can. Take out the trash if it's full. Open the windows if it's too hot.

The inverted statements are sensible of course but they begin to put a lot of weight on the conditional and make you think harder about the potential interpretation(s) of the statement. I also think postfix unless is much easier to follow than the block version.

Go dancing unless you're tired.

Though being Perl, I don't mind seeing the other constructs and don't blame anyone for preferring them. :)

Replies are listed 'Best First'.
Re^2: A matter of style: how to perform a simple action based on a simple condition?
by Tux (Abbot) on Sep 26, 2010 at 18:27 UTC

    The condition heavily weighs it's position quite often. Compare with:

    If you need milk, go and buy some If the trash is full take it out If it is too hot, open the window

    In the first, the condition is now more important than how to meet it. (At least in my perception. In the latter two, I just switched and personally think it is clearer that way. Note that English is not my native tongue and that I am very well aware that not all people think alike. That is why I love perl5: it is very easy to write it as it best fits your mind while still not too hard to read when others wrote it the way they think is best.

    In the third example it is also important to realize the number of options on either side of the and/or/if/unless.

    If it is too hot, turn down the heating If it is too hot, open the window If it is too hot, turn on the air conditioning If it is too hot, go out for some fresh air

    Of course this also goes the other way: there may be a number of reasons to open the window (other than to throw out ... from the 13th floor).

    Enjoy, Have FUN! H.Merijn

      You sort of describe what I see as the main reason to prefer the postfix. “If it is too hot,” just for one example has a dozen, if not nearly infinite, possible remedies, as you note. Drink cold water, take sleeping pills to remain unconscious through it, buy a swamp cooler, shoot yourself to end your misery, fill the oceans with iron powder… The action matters far more than the condition. The condition initiates extra mental effort when it comes first. It creates a theoretical, if not actual, decision tree. You can race ahead to what you think the action should/will be.

      It's also, as noted by many, noisier and generally multiline, to do if blocks which also increases apprehension impedance. Again, in my view. It's Perl, we can do what we wish. I think most of the above replies in favor of the block are more about justifying one's comfort zone, a valid thing to care about, versus actually considering what might be easier to write and read in the simple case (I would never advocate postfix for a logic tree or situations with several actions and conditions in the statement).

      It's funny you mentioned native languages because the same thing occurred to me. I am curious if the SVO order of English—or even lazy idiomatic regionalisms of which I'm the victim—have any bearing in the perception of which is “easier.”

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2021-01-20 13:42 GMT
Find Nodes?
    Voting Booth?