Zero tolerance

This has been surprisingly useful at keeping my JS code clean.


desc "Check for tabs and trailing spaces"
task :crapcheck do
  Dir["public/**/*.js"].each do |f|
    next if f.match(/^lib|resources/)
    text = File.read(f)
    raise "Tabs found in #{f}" if text.match(/t/)
    raise "Trailing spaces found in #{f}" if text.match(/ $|    $/)
  end
end

The tab check is useful because I hate them, and they mess with JSLint; the trailing spaces mess up my diffs. Take that.

Tagged

3 thoughts on “Zero tolerance

  1. Oliver says:

    I’ve been doing exactly the same thing with my Puppet modules/manifests for at least a year now – it certainly raises the ire of those who prefer hard tabs but I think the sheer benefit of readable diffs is enough justification.

    I also check for windows line endings (you can never be sure who is developing on the devil’s platform), files in the right place but with incorrect suffices (e.g. ERB templates) and so on. A good idea all round.

  2. Will says:

    While I’m all for clean code, this reminds me of something that long irritated me with code-check tools like this that were mandated at a previous job.

    Instead of raising errors when things like whitespace or tabs occur, why not just modify the file to correct it?

    What benefit is there to processes which ‘raise the ire’ of co-workers, when a less aggravating and time-consuming option exists?

  3. […] breaks your formatting rules? Should you detect and fix them? There were two comments on the previous post: Oliver agreed, and Will didn’t: Instead of raising errors when things like whitespace or […]

Comments are closed.

%d bloggers like this: