Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: Defensive Programming

by seattlejohn (Deacon)
on Jan 15, 2002 at 12:13 UTC ( [id://138846]=note: print w/replies, xml ) Need Help??


in reply to Re: Defensive Programming
in thread Defensive Programming

While I'm a believer in defensive programming in many ways, I can't help but wonder if in this particular case, the cure is worse than the disease. The subroutine's caller is presumably expecting a valid response ($this or $that, say), yet the code has a fallback case where the routine simply returns something... who knows what. This strikes me as a potential debugging nightmare, because if something gets fouled up in the sub, the program might not break right away -- the sub would just return some (probably bogus) data, which could keep getting processed until it causes some bug way later in the program. Such bugs are, in my experience, extraordinarily hard to track down, because it's hard to know where they even originated.

If I were worried about the possibility that I might forget to explicitly return a value from within one of the branches of my if statement, I'd probably code the sub more like this:

sub blah { if($somevar == $somevalue) { return $this; } else { return $that; } die "Something very bad is happening"; }
which is to say, I would try to scream about the problem as early in the logic chain as possible, rather than sweep it under the rug where someone else might trip over it later.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-19 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found