Something's been bothering me lately. It's
nothing
, really. ?, ?,
null
,
nil
, or whatever you want to call it. I think we've got it backwards in many cases. Many languages like to throw errors when you try to use
nothing
as if it were something else - even if it's nothing fancy.
I think a better default behavior would be to
do nothing - at most log an error somewhere, or allow us a setting - just stop acting as if the world came to an end because I *gasp* tried to use
null
as if it were a normal value.
In fact, just because it's nothing, doesn't mean it can't be something. It
is something - a concept at the minimum. And there's nothing stopping us from having an object that represents the concept of
nothing
.
Exceptions should be thrown when something exceptional happens. Maybe encountering a null value was at some time, exceptional. But in today's world of databases, form fields, and integrated disparate systems, you don't know where your data is at or where it's coming from - and encountering
null
is the rule, not the exception.
Expecting me to write
paranoid code and add a check for
null
to avoid every branch of code where it might occur is ludicrous. There's no reason some sensible default behavior can't be chosen for
null
, and if I really need something exceptional to happen, I can check for it.
Really, aren't you sick of writing code like this:
string default = "";
if(form["field"] != null and boolFromDBSaysSetIt != null
and boolFromDBSaysSetIt)
default = form["field"];
when you could be writing code like this:
if(boolFromDBSaysSetIt)
default = form["field"];
I think this is especially horrid for conditional checks. When I write
if(someCase)
it's really just shorthand for
if(someCase eq true)
. So why, when
someCase is
null
or not a
boolean
should it cause an error? It's not
true
, so move on - don't throw an error.
Someone tell me I'm wrong. It
feels like I should be wrong. But it feels worse to have the default path always be the one of
most resistance.
What do you think?
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
There are no comments for this entry yet.
Leave a comment