|
|
|
|
|
by bluesnowmonkey
5331 days ago
|
|
Having recently worked with a Ruby codebase where anything could be nil at anytime, I consider even references to nil to be a code smell. Developers are inherently prone to ignoring error handling, so they'll happily ignore the fact that Order#customer really means Order#customer_or_nil. And when they see errors in production, they'll slap an andand on it and call it fixed. The code becomes extremely difficult to reason about. I prefer that functions throw an exception when unable to do what they promised, rather than return nil or a null object. The try-catch block serves as documentation that the function might fail. If someone forgets to catch the exception, it will shut everything down rather than leave the program in an unanticipated state that could lead to an error elsewhere. |
|
Scala's standard library provides very helpful information on how to replace null with its Maybe class (called Option in Scala). Just take a peek into their collections library[4], and search for Option.
[1] http://www.infoq.com/presentations/Null-References-The-Billi...
[2] http://moonbase.rydia.net/mental/writings/programming/monads...
[3] http://andand.rubyforge.org/
[4] http://www.scala-lang.org/api/current/scala/collection/immut...