WideFix tech post

Ruby hash styles formatting

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

Variants of using

Asume we have to write some method build_location which receives hash object with a lot of default parameters. So in this case we have a few variants how to format this peace 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 infinite line of parameters. It is too difficult to find something in it and add new parameter. Block #2 looks rather pretty, but if you want to change parameter name of add one you will have to format them again. So this code is too difficult to support also. So my choose is #3.

The best one

In block #3 we have easy supported code, if we change list of parameters or change name we won’t have to change anything with hash parameters. Code like this I will chose not only for styling method parameters but for any place in code where I have hash with list of many parameters. To confirm this assertion let’s try to change name of parameter and add new:

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 that what I wanted without any regression issues for formatting my code.

Additional resources

A lot of code examples for this theme you can find in book Code Complete, second edition by Steve McConnel. This is a huge book, but it is very interesting, important, and contains a lot of advices for coding. Also it includes in list of books which must read every programmer. Thank you, Steve, for your hard work!

Code Complete, second edition. By Steve McConell</img>
Are you seeking assistance with Ruby on Rails development?

Read also