How can a Ruby style guide help you write better code?
Have you ever written code as part of a team and been frustrated by the inconsistencies between code styles?
Or perhaps you’ve had that colleague who was borderline OCD when it came to an 80 character line length. And they weren’t afraid to let you know that you’ve exceeded it.
You know a code style guide is a great thing to have in your team/project. The problem is that it can be a pain to stick to consistently. Particularly when you have people contributing with various skill levels and backgrounds.
Onboarding new people into a project only exacerbates the problem. Your pull requests begin to fill up with style guide violations and you start feeling like a troll, perhaps leading you to overlook these violations to avoid becoming ‘that person’.
Through my consultancy gigs, I’ve worked on a lot of other people’s code bases over the years, I’m a big advocate of having a style guide and finding ways to ensure you can stick to it. It makes life so much easier to come into a well organised code base that has clear guidelines.
Also, if you’re just starting out as a Ruby developer then it’s a great idea to read the Ruby style guide and let it teach you some good habits.
The problem is, you’re probably going to read it once or twice and then start writing some code, then before you realise it, you’ve forgotten what the guide says. It takes time to go to the guide and read it again so you probably don’t check and it doesn’t take long for the variations to creep in.
We must be able to automate it right?
Yes, we can automate this… Rubocop to the rescue.
Rubocop is a static analysis tool for Ruby that will compare your code to a set of rules and notify you of any violations. This is exactly what we want.
By default it will use the community maintained Ruby styleguide as it’s base set of rules. If you want to customise it, that’s easy to do too.
The great thing about Rubocop is that you can run it as part of your test suite and get notifications about style violations before you commit your code. No more
line length comments in your pull requests. :)
How to setup Rubocop to play nicely with your test suite
For Rubocop to do it’s job consistently, you need to make it run as part of your test suite. If you had to run it manually using the
rubocop command then I suspect you’d quickly forget to run it and never get the benefits.
The easiest way to automate this is to use the provided rake task.
Rakefile just add:
require 'rubocop/rake_task' Rubocop::RakeTask.new
Then every time you run
rake (the default rake task runs your test suite), Rubocop will run first and fail if there’s any violations.
If Rubocop isn’t your thing and you want to publicly shame people into sticking within the style guide, you could give Hound a try.
It’s a service from Thoughtbot that runs Rubocop for you when you submit a pull request and outputs the violations as comments on your PR.
The benefit being that you don’t need to install Rubocop yourself.
Personally, I prefer to keep that stuff out of pull requests, as it’s just noise. YMMV.
Don’t want to use Rubocop at all? Try Cane, a project from the team at Square for code quality checking. Not a direct alternative to Rubocop, but if you want a lightweight way of checking code complexity and style violations, this may work for you.