A common idiom in ruby is to call a method only if its receiver is not nil:
thing.foo if thing
or:
thing && thing.fooVarious libraries exist for making this a little more convenient. You can use andand, or if you are using Facets you can use ergo. And seriously, you should be using them – they make your code cleaner and more succinct.
But don’t let the existence of these libraries give you the idea that having null-checks throughout your code is OK. It’s not. Pervasive null tests are a code smell.
If you find you are using the elvis operator or its equivalent everywhere, you most likely have a design problem. Is it really OK for that attribute to ever be null? In a lot of cases the existence of a null attribute or association is indicative of an insufficiently specified contract. Maybe you should be setting that attribute to a default value at initialization, or requiring an explicit value for it at initialization. If it really is OK for that attribute to be null, consider whether you should be using a Null Object or some kind of default placeholder object rather than generic nil.
Checking for null is almost always an implementation detail, not a part of the domain you are modeling. As such the null check should be isolated to the boundaries of your code, or eliminated altogether. Code that constantly checks if things are null has an insecurity complex, always second-guessing itself. Make your code self-confident. Eliminate null checks wherever you can.
Add New Comment
Viewing 12 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Maybe the solution is to add a presentation layer that take care of this conditional nil check?
Do you already have an account? Log in and claim this comment.
But Rails views are smelly in general, because as the least-OO part of Rails they are the hardest part to apply conventional OO patterns and idioms to.
Do you already have an account? Log in and claim this comment.
First, being paranoid (or having an insecurity complex) during development can help prevent code from "working" until the 11th hour. Healthy use of asserts to check for NULL's in this case can be a good thing. Secondly, not all problem domains are the same. In the embedded and/or safety critical space, severe damage can result based on unexpected values propagating through code even during development. By working with the first point, significant and costly damage can be avoided (and, in my case, has been more than once.) So, in my case, I try to balance the compulsive (can you say OCD anyone?) checking against out of bounds values against the impact to performance. Specifically, I use assert() heavily to validate initial code passes in development and then disable the assert() when I judge code to be more stable. Of course, this is always a judgment call for any coder/designer.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
I agree 100%
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
As for using one representation for two concepts, that's a valid argument, which is behind the anti-NULL-ers like Chris Date. I'm on the other side of that argument (the Codd side) and have no problem with the idea that nil represents a "non-value," especially in favor of fabrications like 9/9/99. That's what nil was designed to do. I've used languages without it (e.g. C) and I'm glad to have it. But as I indicated above, this isn't an argument that's going to get won any time soon. :)
Add New Comment