Kerry Buckley

What’s the simplest thing that could possibly go wrong?

Archive for the ‘comments evil ruby httparty magic’ tag

Comments aren’t always evil

4 comments

I tend to agree that comments are, in most cases, evil (or at least mildly malevolent), but I did come across one of the exceptions to the rule today.

While doing a bit of drive-by refactoring while fixing a bug, I reflexively changed this line:

RUBY:
  1. unless instance_response.nil?

to this:

RUBY:
  1. if instance_response

Then reading the comment above the line, expecting to delete it, it all came flooding back:

RUBY:
  1. # Use instance_response.nil? to check if the HTTParty
  2. # response's inner hash is empty.
  3. # If you use 'if instance_response', it is always true.

Now you could maybe argue that this unexpected behaviour is because httparty uses just a little too much of that old method missing proxy magic (which of course isn't really magic at all), but that's not the point of this post.

In the end I pulled it out into a private method to make it clearer what was going on, but decided to leave the comment in.

RUBY:
  1. def self.instance_returned? instance_response
  2.   # Use instance_response.nil? to check if the HTTParty
  3.   # response's inner hash is empty.
  4.   # If you use 'if instance_response', it is always true.
  5.   !instance_response.nil?
  6. end

Written by Kerry

August 14th, 2009 at 9:21 pm