Find an article

Start typing to search the archive.

← All articles

Formatting Ruby hashes

Advice on how to improve hash formatting in Ruby/Ruby on Rails code.

Have you ever felt annoyed when formatting hash keys and values in Ruby code? So have I. And I’m going to describe how to improve it in this post.

Formatting options

Assume we have to write a method, build_location, that receives a hash object with a lot of default parameters. So in this case, we have a few ways to format this piece of code:

  • Inline (1)
def build_location(params = {:first_name => 'FirstName', :last_name => 'LastName', :address => 'DefaultValue', :phone => 'PhoneNumber', ...})
...
end
  • Pure blocks (2)
def build_location(params = {:first_name => 'FirstName',
                             :last_name  => 'LastName',
                             :address    => 'DefaultValue',
                             :phone      => 'PhoneNumber', ...})
...
end
  • Using begin-end Pairs (Braces) to Designate Block Boundaries (3)
def build_location(params = {
    :first_name => 'FirstName',
    :last_name  => 'LastName',
    :address    => 'DefaultValue',
    :phone      => 'PhoneNumber', ...
})
...
end

Selection

Block #1 seems disgusting because we have an endless line of parameters. It is too difficult to find something in it or add a new parameter. Block #2 looks rather pretty, but if you want to change a parameter name or add one, you will have to format them again. So this code is also difficult to maintain. My choice is #3.

The best one

In block #3, we have code that is easy to maintain. If we change the list of parameters or a name, we won’t have to change the formatting of the hash parameters. I would choose code like this not only for styling method parameters, but anywhere in the code where I have a hash with many parameters. To confirm this assertion, let’s try to change a parameter name and add a new one:

def build_location(user, this_is_default_params = {
    :first_name => 'FirstName',
    :last_name  => 'LastName',
    :address    => 'DefaultValue',
    :phone      => 'PhoneNumber', ...
})
...
end

As you can see, I’ve changed only what I wanted without any regressions in my code’s formatting.

Additional resources

You can find a lot of code examples on this topic in the book Code Complete, second edition by Steve McConnell. This is a huge book, but it is very interesting and important, and contains a lot of coding advice. It is also on the list of books every programmer must read. Thank you, Steve, for your hard work!

Code Complete, second edition. By Steve McConell