The Coding Style Guide

06 Oct 2014

When you’re working with a team of developers, it’s a good idea to set the ground rules for coding style.

Indentation, whitespaces, new lines, line width, file size, etc. usually don’t get the credit they deserve. The compiler doesn’t care so why should you? Well, the compiler uses a bunch of regular expressions to detect patterns easily, but your eye can’t, so we must use the above elements to make it easy for us to read and understand our code.

Example:

def connection;@connection||=Faraday.new(:url=>SOME_ENDPOINT){|faraday|faraday.use FaradayMiddleware::FollowRedirects,limit:3;faraday.response :logger if ENV['DEBUG];faraday.adapter :net_http};end

Really hard to understand, however with whitespaces, line breaks and proper indentation:

def connection
  @connection ||= Faraday.new(:url => SOME_ENDPOINT) do |faraday|
    faraday.use FaradayMiddleware::FollowRedirects, limit: 3
    faraday.response :logger if ENV['DEBUG']
    faraday.adapter :net_http
  end
end

We can see that even simple code becomes unreadable without proper formatting, taken to extremes it’s easy to understand the negative effect and why you should care.

There are many language-specific practices, for Ruby I like this style guide. You can look up one for the programming language you use, or even define one with your teammates. When you agree on a style guide, stick to it. You want anyone in the team to be able to understand and contribute to the project.