Stok Footage

Continually experimenting with new ideas and techniques — Reconstructing, Developing, Modernising.

Cleaning up an old code base (with syntastic’s help)

I am attempting to make the idea of shared code ownership more popular at $work, and one of the “irritants” is the variety of formatting people manage to do — for example the use of hard tabs with the assumption that tab stops are two columns apart … fine if we all have our editors set up the same way, but the bulk of external tools assume eight column tab spacing and the code suddenly becomes very wide outside the editor. Rubocop was an excuse for me to use “community standards”, and to try and find a way to enforce them.

I managed to integrate rubocop and syntastic into the workflow I use on a Ruby 1.8.7 code base at $work, and I found it the inclusion of syntastic in my vim setup to make a huge difference to the way I worked.

Syntastic provides almost immediate feedback when I save a file, so if I’m working in a “clean” file and syntastic creates a gutter on the left of the screen then I know that I need to look at something. This automated feedback helps me keep things tidy as I write, much like guard helps me keep my tests passing by running them as files are updated.

I have used rubocop for ages at home, and it has been easy to integrate because I am using supported Ruby versions — that is more recent than 1.8.7 — with chruby to switch, so there was always a rubocop lurking on my path as long as I remembered to use it. Rubocop is a essentially a creature of Ruby 1.9+

So I had three main things to sort out: a Ruby 1.8.7-ish .rubocop.yml file, make sure that rubocop runs even if my .ruby-version file specifies a 1.8.7 version, and helping my co-workers get into the habit of making use of syntastic.

The basis I use for a 1.8.7-ish rubocop.yml file is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Ruby 1.8.7 needs the . on a chain of method calls at the end of a line.
Style/DotPosition
:
  EnforcedStyle
: trailing

# Ruby 1.8.7 doesn't have the -> lambda
Style/Lambda
:
  Enabled
: false

# Ruby 1.8.7 doesn't have 1.9 style hash syntax
Style/HashSyntax
:
  EnforcedStyle
: hash_rockets

# This is my personal preference...
Style/StringLiterals
:
  EnforcedStyle
: double_quotes

To make rubocop work in my environment when I was in a directory with a .ruby-version file I created the following executable rubocop in my bin directory. It assumes I have installed ruby 2.1.2 and the rubocop gem for ruby 2.1.2, and it works with chruby; if you’re using rbenv or rvm you’ll need to invent your own.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
#
# Wrapper for rubocop so that I can use it on projects which use older rubies
# for which there is no rubocop available as a gem.
#
# chruby puts its gem & ruby bins at the beginning of the path, so the real
# rubocop ought to be found first.  The reason for having the full path is to
# avoid ~/bin/rubocop calling itself if I have forgotten to install the rubocop
# gem for the specified ruby gem.

set -e

ruby_version=2.1.2
source /usr/local/opt/chruby/share/chruby/chruby.sh
chruby $ruby_version
exec "$HOME/.gem/ruby/$ruby_version/bin/rubocop" "$@"

Finally to get syntastic integrated with vim, using vundle, I suggested adding these lines to our .vimrc files:

1
2
3
4
" Syntastic...

Plugin 'scrooloose/syntastic.git'
let g:syntastic_ruby_checkers = ['rubocop']

… and then we were all set.

2 Responses to “Cleaning up an old code base (with syntastic’s help)”

  1. Austin Wood says:

    Hey, thanks for the post. I have been looking for a good way to integrate rubocop with my vim syntastic linting. However, this line:

    let g:syntastic_ruby_checkers = [‘rubocop’]

    Seems to cause significant lag in my file when I save. Have you experienced this issue at all? I don’t know if I just need to get used to the delay on save, or if there is a fix for this.

  2. MikeStok says:

    I have found there is a little delay saving the file, for me it’s typically around a second and I’m looking at another window to see what guard spits out so I’m not too bothered by it now I’m used to it.

Leave a Reply

Your email address will not be published. Required fields are marked *