Is your code littered with checks for types and nulls? Don't get me wrong, now - coding defensively
is quite alright:
some_number = get_user_input()
some_number_plus_3 = some_number + 3
So is a bit of polymorphism:
input = input.join("") if(input is an array)
With that you can treat arrays and string inputs the same. (This comes from an example where I actually wanted to do the opposite, in messing with Smith-Waterman
Maybe you have certain default parameters to your program and you'd like the ability to override some of them with a tag in an XML file, as I was doing recently. You get an
by searching the
for that tag. Then, if
, you use the default.
But if you are constantly checking types and asking if
to the point at which the signal to noise ratio is so low as to be annoying, your code might be irrationally fearful, anxious to the point of delusion. You might even ask if the time has come for it to visit a professional trained in treating paranoia
Is your code paranoid? Or am I just too liberal with mine? Thoughts below are encouraged.
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
this is why you create a class and do all your checking an validating in the class itself so you don't have all this business logic all over the place. plus you look less paranoid and more geekish.
Posted by tony petruzzi
on Oct 02, 2007 at 05:31 PM UTC - 5 hrs
I find that when i code "too" defensively I can end up covering up bugs that I'd rather be finding.
Posted by Joe Zack
on Oct 02, 2007 at 06:31 PM UTC - 5 hrs
I tend to think a bit like Joe. I make an effort to distinguish the scenarios that really might happen from those that I think are highly unlikely. It's hardly scientific, I admit, but it keeps me from being ridiculous. And if I'm wrong - especially about the places where I don't think it's likely and it happens anyway, the fatal error tells me right away.
Even more ghastly...sometimes I intentionally avoid error handling for the same reason. There are just some places where, if there's a problem, I want to no about it immediately and in no uncertain terms.
Posted by Rob Wilkerson
on Oct 02, 2007 at 07:03 PM UTC - 5 hrs
Leave a comment
Tony - funny you mention that, I had written the same earlier that day!
Joe - Do you have a concrete example where that happened? Are you talking about something so paranoid as try/catching your entire program?
Rob - I think we're on the same page there.
Is there a place it turns from anti-paranoia to sloppiness though?
Posted by Sam
on Oct 05, 2007 at 09:00 AM UTC - 5 hrs