Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: Re: Defensive Programming

by demerphq (Chancellor)
on Jan 14, 2002 at 21:24 UTC ( #138622=note: print w/replies, xml ) Need Help??


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

Oh I agree completely. I actually originally put a line like "and in perl you have even better options with the modifiers that are available" but it didnt quite fit so I took it out. Perl modifiers and control flow keywords are an excellent way to minimize excessive indentation and containment problems.

Nevertheless I do think that "the smaller part of a conditional should go first" is a correct rule of thumb. If you have a 4 line block and a 40 block then the 4 liner should go first. This means the maintainer can see all of one and part of the other on one page, whereas with the long block first the maintainer can only see part of one block, and may not even notice the presence of the smaller one.

Yves / DeMerphq
--
When to use Prototypes?

Replies are listed 'Best First'.
Re (tilly) 4: Defensive Programming
by tilly (Archbishop) on Jan 14, 2002 at 23:38 UTC
    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.

      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://138622]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2022-05-21 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (78 votes). Check out past polls.

    Notices?