Sunday, June 08, 2014

Ruby Science Code Review

This article is a discussion of the chapter on Replace Conditional with Null Object. Null object does not solve any problems in most cases. For instance here is the sample code from the book:

if user.nil?
  nil
else
  user.name
end

If I cannot use Null Object (which creates unnecessary small objects). What is the right way to fix this? You need to apply the basics of Object Oriented Programming.  Ask yourself:


  • What does it mean to have a user variable with nil value?
  • If user does not exist in our system what do you want to display instead of their name? 
  • Is blank value acceptable in this case?


 If the above code snippet is part of a method, then the precondition for the method is that user object cannot be nil. It is a contract between the client and the API. As l long as I provide a valid user the method will be able to do it's job (fulfill the contract in Design by Contract terminology) and provide me the name. It's the responsibility of the client code to provide the method a valid user. In this case the solution would be:

Client Code

if user.nil?
  'User not found'
else
   user_name_for(user)
end

API

def user_name_for(user)
  user.name
end

Ideally I would look for the ActiveRecord call that throws an exception instead of returning nil for a record that is not found. I would handle the ActiveRecord::NotFound exception and translate it to business specific exception like MyApp::UserNotFound class that takes the message indicating the context of the application as the exception message. So there would be no nil check required in this case.