Phlip
2/21/2009 7:29:00 PM
David A. Black wrote:
> Definitely use #nil? if you want to know whether something is exactly
> nil. There are certainly a lot of unnecessary calls to #nil? out there,
> but it doesn't follow that it can never be right to test for nil.
Let's call this the "non-suicidal Samurai Principle". Either return victorious,
or return empty-handed. (And don't waste my expense training you, duh!)
How many times have we written...
def samurai
blah and blah or blah
end
if samurai
...
....and then never bothered to check - even with unit tests, whether a failing
samurai() call returned a nil or a false? Ruby's break with incorrect tradition
- letting 0 be true - cleaned up a whole lot of clutter. .index() can easily
distinguish "found at 0 index" from "not found". But this means we no longer
always need to track which of those blah() calls returns a nil, which a false,
and which one the boolean short-circuiting collects for us.
--
Phlip