Daily WTF: NilClass#method_missing

code | nil | ruby | wtf April 18 2008

With great power comes great responsibility.

I’ve been leading Ruby and Rails training over the last couple weeks for a company in Brisbane, Australia, Xerox in El Segundo, CA, and yellowpages.com in Glendale, CA. Visiting companies is always so interesting because you get to see how different people work together and how they go about solving problems. You also get to see some really interesting code.

I was helping one of the companies walk through some code they inherited from an acquisition and came across something similar to this wonderful snippet of code:

  class NilClass
    def method_missing(method, *args)
      raise(NoMethodError, "undefined method '#{method}' for nil")
    rescue => e
      CustomLogger.error(e)
      nil
    end
  end

WTF? They’re overriding #method_missing on nil to raise an exception, they then proceed to rescue it, log the error, and return nil!

I’m guessing they had some buggy code that was calling a method on nil, causing a NoMethodError to be raised. And instead of fixing the code, they just decided to change it so that you could call methods on nil and it would just silently fail.

>> nil.foo.bar.baz
=> nil

It took me a while to figure out why they were raising an error and then rescuing it, but I’m guessing it’s so they have access to a stack trace in the logger.

I’ll give them credit for being creative, but whoever wrote that piece of code doesn’t deserve to be using Ruby. Yes, Ruby’s open classes are powerful, but that’s just ridiculous.

posted by brandon | updated April 18th 03:28 AM
comments feed

5 comments

  1. Apart from the fact the strack trace is always available via caller.

    Stephan Stephan
    April 18, 2008 at 03:38 AM
  2. The strack trace is always available via caller anyway.

    Stephan Stephan
    April 18, 2008 at 03:38 AM
  3. I completely agree. I found this in LovdByLess (which has been getting a lot of attention lately):

    http://github.com/stevenbristol/lovd-by-less/tree/master/vendor/plugins/less_monkey_patching/lib/nil.rb

    WTF?

    Brian Brian
    April 18, 2008 at 05:18 PM
  4. Spend enough time with a complex Rails app, and you’ll understand why this happens.

    This is often a fairly reasonable thing to do. Log the event, return nil to (usually) the view rendering code so that the app doesn’t 500 for your users, and follow up on the log messages and fix yr damn code. :)

    andrew andrew
    June 24, 2008 at 02:38 PM
  5. andrew:

    I’ve spent every day of the last 2 years working on Rails apps. I don’t know that I’ve worked on a project yet that I wouldn’t classify as “complex”.

    Why stop at overriding #method_missing on NilClass? Why not just override it on Object so that any object responds to any method? That would make sure that you never get an error message!

    If your code is hitting a condition where it would raise an error, then it’s probably not doing what the user expects anyway. Why pretend like nothing is wrong? Your users would appreciate if you quit wasting there time and just acknowledged that there is an error and you’re working on fixing it.

    Overriding #method_missing on NilClass in this manner is simply ridiculous. It is the symptom of poor programming.

    Brandon Brandon
    June 24, 2008 at 05:10 PM

Speak your mind:

(Required)

(Required)


(You may use textile in your comments.)

About

I'm Brandon Keepers, a web application developer that likes beautiful code, valid markup and adherence to standards. As a part of Collective Idea in Holland, Michigan, I practice Agile software development primarily using Ruby on Rails.

-86.103171 42.785037

Contact:

more ยป

Syndicate