Susan Potter
snippets: Created / Updated

Ruby Idioms, Part 6: Postfix conditional flow

Tonight I remembered couple more idioms in Ruby and felt compelled to share.

In Javafied Ruby code (below) we often see something like the following:

    if something
      for role in user.roles
        return true if @@roles.include? role
      end
    end

Now when I first started writing Ruby code almost 3 years ago, I thought looping through a collection like above was the nicest way. I quickly found Ruby like the following snippet in code I inherited:

    user.roles.each do |role|
      return true if @@roles.include? role
    end

At first it took me some time to understand it (remember I was just a poor Java developer with a little Python experience), and I wondered why people would write loops this way. At the time it made no sense. Using this form of looping in Ruby does somewhat violate my "reads more like natural language" coding style preferences, but for some reason this form of looping is now the only one I use in Ruby code. Despite this violation, it feels much more natural. If you are unsure about this yourself, I would encourage you to look into the power of blocks in Ruby. Once you fully appreciate blocks, I think you might be persuaded, (though different opinions make this world more colorful and less bland).

The second Ruby idiom is less odd and does enhance code readability more like natural language (again in my opinion):

    user.roles.each do |role|
      return true if @@roles.include? role
    end if something

Usually when people are having a conversation in English (at least native English speakers), we tend to say things like: We need to verify at least one role the user belongs to is in a pre-defined list of roles if [something]. Non-techies tend to say "if something" at the end of sentences, not at the very beginning (at least this is my experience). So for this reason I think this idiom enhances readability of the code to be more like natural English. For those that start every sentence that contains a conditional with the conditional clause, you have probably been a developer for far too long - go be a beach bum to save yourself before it is too late.

If you enjoyed this content, please consider sharing via social media, following my accounts, or subscribing to the RSS feed.