# Null checks in Ruby

**URL:** <https://www.ruby-forum.com/t/null-checks-in-ruby/191789>\
**Category:** Ruby\
**Created:** [July 18, 2010, 9:56pm UTC](https://www.ruby-forum.com/t/null-checks-in-ruby/191789 "2010-07-18T21:56:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [July 18, 2010, 9:56pm UTC](https://www.ruby-forum.com/t/null-checks-in-ruby/191789/1 "2010-07-18T21:56:29Z")

</div>

I am working to learning Ruby and have a bit of a question regarding  
program structure.

In Java we typically checked (at the beginning of a method) for null and  
that the parameters being passed in are of a certain value.

Maybe I am still (very) new to this world but, are null checks done a  
lot in the real world with Ruby?

For example, I have seen a lot of code doing something like

if(obj == null){  
log.error(…);  
throw new SomeException(errMsg);  
}

if(obj2 == null){  
log.error(…);  
throw new SomeException(errMsg);  
}

Does this happen in Ruby programs as well?

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [July 18, 2010, 10:55pm UTC](https://www.ruby-forum.com/t/null-checks-in-ruby/191789/2 "2010-07-18T22:55:32Z")

</div>

On 18.07.2010 21:56, Carl J. wrote:

> I am working to learning Ruby and have a bit of a question regarding  
> program structure.
> 
> In Java we typically checked (at the beginning of a method) for null and  
> that the parameters being passed in are of a certain value.

That does make sense although often null checks are superfluous because  
the JVM will do it for you once you use a null reference. You only need  
the explicit null check if you want to throw another exception or check  
the argument before beginning of the work (i.e. modifying some state).

> Maybe I am still (very) new to this world but, are null checks done a  
> lot in the real world with Ruby?

Probably less often than in Java. Some of them are actually hidden,  
e.g. in the typical idiom

h = {}  
…  
h[key] ||= []

the ||= operator in this case does an implicit check for “nil” (or  
“false”).

> }  
> This is bad code IMHO. Reason: either you handle the error by logging  
> it or you cannot continue at this place and throw an exception. If you  
> do both someone up the call chain might catch the exception and also log  
> the error because that is the only resolution he can do. Now you got  
> two log statements for the single error. Even worse, the catcher of the  
> exception may handle it in a way that there is no overall error. Now  
> you end up with an error log statement which is not an error. That  
> costs IO and can send someone looking for the complete wrong cause when  
> looking for the cause of a program’s misbehavior.

> Does this happen in Ruby programs as well?

Most of the time we use what we are passed and simply let the  
interpreter throw an exception if it does not meet our expectations  
(i.e. understand the messages we send to it). Note that in Ruby you can  
actually invoke methods on nil although not many do actually make sense  
(#to\_s is one of them).

See also: Duck typing.

Kind regards

robert

PS: Note also a subtle difference between Java and Ruby: Java’s null is  
actually nothing while Ruby’s nil is an instance:

irb(main):001:0\> nil.object\_id  
=\> 4  
irb(main):002:0\> nil.object\_id  
=\> 4  
irb(main):003:0\> nil.nil?  
=\> true  
irb(main):004:0\> nil.to\_s  
=\> “”  
irb(main):005:0\> nil  
=\> nil  
irb(main):006:0\> puts nil.inspect  
nil  
=\> nil  
irb(main):007:0\>
