Ruby Readable Booleans

There’s a great little trick you can do to improve the readability of your code. A common problem is dealing with methods that have a boolean flag arguments. Here’s an example I ran into just today in a Rails application:

1
2
3
def rating_stars(..., clickable = false)
  # ...
end

The problem with this is that you typically see calls like this scattered around the application:

1
<%= rating_stars(..., true) %>

Would you know what true did there if I hadn’t shown you the name of the variable first? I didn’t. I had to go hunting for that method definition.

Ironically the opposite problem, a magical dangling false, is much more rare in my experience. That’s typically the default for these kind of arguments and it just makes more sense and reads better to leave it out.

Anyway, the point is that we can typically improve the ease of understanding the common case. Remember that one of the new features of Ruby 2.0 is keyword arguments. keyword arguments make it easier create readable method. For example, after looking up the method and understanding what was needed:

1
2
3
def rating_stars(..., clickable: false)
  # ...
end
1
<%= rating_stars(..., clickable: true) %>

So far so good, my hope is that might save a future maintainer a trip to the method definition to understand the call.