How RuboCop Made Me a Fan of Linters

I recently switched to a new project and found myself back in Ruby code. Drew, who set up the project and had been working on it for a few weeks, had taken the time to set up the project’s test suite with something called RuboCop.

Screenshot of Rubocop

What is RuboCop?

First thing’s first: RuboCop is a very fancy linter for Ruby. It reads your Ruby code and prints out a report about all the things you’re doing “wrong.”

At first glance, this was not very exciting to me. I’ve used a linter before. Even if I had not, why would I be happy to have something to nag and point out all the things I’ve done wrong? (Why would I be writing this blog post?)

The answer: autocorrect. In practice, RuboCop is able to automatically rewrite your source code to fix the vast majority of its complaints. This is something I haven’t seen in linters I’ve used in the past, and I’ve been very happy to have it.

Reducing Cognitive Load

For better or worse, I love double-quoted strings. My favorite languages all share this characteristic: strings cannot be made of single quotes. Alas, Ruby. Virtually all Ruby code follows the standard community convention: single quotes unless you need string interpolation.

There’s no good reason for me to fight this. It just happens. The double quotes flow out of my fingers, to the blight of Rubyists everywhere. Now I don’t need to care. RuboCop will fix it later, and spare me the cognitive burden.

Beyond this, I’m learning to stop caring about white space. RuboCop will zap it. Same for misaligned indentation, using do/end instead of curly braces, etc.

Source code is weird in that it’s a textual encoding that co-mingles two very different concepts. The first concept is a mathematical encoding of computation. The second is aesthetic and presentational. I’m far more interested in the former, and I am happy to have a tool to help reduce the burden of the latter.

Learning How to Ruby

Syntactically, Ruby is a very complex language fraught with traps. Incidentally, that makes RuboCop all the more impressive, as its creators first had to tackle the daunting task of parsing Ruby.

Before you can write good Ruby code—or good code in any language—you have to understand the complications created by the language. There are patterns that Ruby encourages through its syntax, such as the implicit use of self. There are fundamental design decisions of the language, such as having mutable strings and data structures. It takes a good amount of experience to reconcile it all into good coding practices.

Fortunately, that’s exactly what RuboCop provides with its linting: the experience of the community.

Did you know about Enumerable#each_with_object? I didn’t, but RuboCop pointed it out. I think it’s a better fit than reduce for most applications. What’s the point of reduce when your reducing function mutates the accumulator anyway?

Even for things I know about, like using save! or create! instead of save or create on an Active Record object, it’s really helpful. RuboCop has my back.

Conclusion

I’m pretty happy that Drew set this up for our project. I’ve become a fan of linting tools, especially when they’ll fix issues for you. If you’re using Ruby, you should definitely give RuboCop a try.