if some_value
do_something
end
It's a core expectation of the language that !!nil_thing == false, so why violate it? if !some_value
do_something
end
you often don't actually want it to be executed when some_value is nil instead of false. And when it is, you go scratching your head and searching where some_value came from, to discover some silly typo.In some ways, the reverse is even worse: when you write
if some_value
do_something
end
and you change some_value from nil to some sensible default like '', you will forget to update this clause and you will not understand why the code is being executed. If you use if !some_value.nil?
do_something
end
you don't have that problem, because the nil-test sticks out like a sore thumb.Somehow this has never been a problem for me in eight years and hundreds of thousands of Ruby lines--but every programmer is different, and I'd like to hear other views.
change the base case of some_value from nil to 0, nil to NullObject
change some_value from nil to some sensible default like ''
What, what, what are you doing? Who uses an empty string to mean "uninitialized"? Nil and {0, "", etc) have very different semantic meanings. How would you discriminate between the default "" and a user-submitted ""? If you mean to check whether it has changed from the default, compare it to a default constant. If you mean to check emptiness, use #empty? or #blank?. I read these kinds of explicit .nil? checks as fighting the language, and in some ways, a violation of the cultural contract in Ruby and Lisp around truth charge.
This could lead to a tri-state ifs (with true/false/nil branches).
Or you can just consider that 2-branch 'if' runs the first branch if the test expr is true and the second 'otherwise'. (So that doesn't make nil false, it just makes it non-true).
And then your interpretation and everyone else co-exists and we can all code together.
What stuff should happen when something is unexpectedly nil if you tested it for truth-ishnes? Once you have written
if not some_value
something with some_value
end
the only things you can do with `x` are things you can do with `false`, which are not many. if some_value
something with some_value
end
where you first use nil to signal false, but later change the base case of some_value from nil to 0, nil to NullObject or nil to some other base value. Since you think of these base cases as 'nothing', you will forget to change this related conditional the first time around, because it seems it would still do the right thing.
If you expect some_value to be able to be nil (which, for boolean values, is hardly ever), you should write
In the end, these 'convenient' coercions that allow you to use any value as a boolean only come back to bite you, because unexpected stuff happens when stuff is unexpectedly nil. If it makes my coworkers cry that an object is nil and true at the same time, then they are at fault, not me.Edit: and more importantly (I forgot to point this out explicitly) a non-nil value != true. That any string or integer is 'true' can lead to very hard-to-find bugs.