Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re (tilly) 4: Defensive Programming

by tilly (Archbishop)
on Jan 14, 2002 at 23:38 UTC ( #138668=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Defensive Programming
in thread Defensive Programming

The rule, "the smaller part of a conditional should go first" conflicts with the rule, "the expected path should go first". The justification for the second rule is that it makes the code more natural to skim. That is, of course, secondary to wanting to make it clear what is part of the conditional. But it is not totally irrelevant.

I usually put the expected result first, and avoid the size issue by moving the body of long blocks into functions. That probably works 95% of the time. The exceptions are mainly cases where I have nested control logic that I want to keep in one place, or situations where I need to inline a long piece of text (eg an SQL statement). Then I will move the shortest block first for the reason that you give.

So my rules in order of precedence are to make it obvious what belongs to the conditional, and then to make the most common flow of the program match the natural flow of skimming. But the more important can usually be satisfied without harming the second, and I do.

Replies are listed 'Best First'.
Re: Re (tilly) 4: Defensive Programming
by demerphq (Chancellor) on Jan 15, 2002 at 13:56 UTC
    Yes. I would agree that one needs to balance the two points you made. Personally however I tend not to put unreusable code in functions so I probably give the size rule slightly higher precedence as a consequence. But when the code blocks are not signifigantly different in size the expected result first is the rule I go with as well.

    Yves / DeMerphq
    --
    When to use Prototypes?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-05-26 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (93 votes). Check out past polls.

    Notices?