Null pointer (sometimes written NULL), used in computer programming for an uninitialized, undefined, empty, or meaningless value (More).
null is meaningless. Yet, how many times have you seen implementations like this?
if !accounts.nil? accounts.each do |account| account.do_something end end
accounts had been an empty collection (
) we could run the above block without the
accounts.each do |account| account.do_something end
That’s because a
Collection object is prepared to respond to the
:each message, even when it’s empty. Your objects, too, should know how to respond to the messages passed.
type = nil output = OutputDevice.find_by_type(type) output.print "This shouldn’t print anywhere"
OutputDevice implementation returned
nil when it doesn’t find an appropriate output device based on
type, we’d get an error for trying to pass it the
output.print "Without the nil check, this breaks" unless output.nil?
Remember the purpose of
nil is to represent the concept of meaningless or empty.
Null […], used in computer programming for an uninitialized, undefined, empty, or meaningless value.
If we instead return a
NilOutputDevice object (for example) that responds to
class NilOutputDevice def print(message) # do nothing, discard message end end type = nil output = OutputDevice.find_by_type(type) output.print "This shouldn’t print anywhere"
In this simple example it makes more sense to just remove the