Should we enforce code style in our large codebase?

Posted by eighttrackmind on Programmers See other posts from Programmers or by eighttrackmind
Published on 2014-08-18T16:56:57Z Indexed on 2014/08/18 22:31 UTC
Read the original article Hit count: 639

By "code style" I mean 2 things:

  1. Style, eg.

    // bad
    if(foo){ ... }
    
    // good
    if (foo) { ... }
    
  2. Conventions and idiomaticity, where two ways of writing the same thing are functionally equivalent, but one is more idiomatic. eg.

    // bad
    if (fooLib.equals(a, b)) { ... }
    
    // good
    if (a == b) { ... }
    

I think it makes sense to use an auto-formatter to enforce #1 automatically. So my question is specifically about #2. I like to break things down into pros and cons, here's what I've come up with so far:

Pros:

  • Used by many large codebases (eg. Google, jQuery)
  • Helps make it a bit easier to work on new areas of the codebase
  • Helps make code more portable (this is not necessarily true)
  • Code style is automatic once you get used to it
  • Makes it easier to fast-decline pull requests

Cons:

  • Takes engineers’ and code reviewers’ time away from more important things (like developing features)
  • Code should ideally be rewritten every 2-3 years anyway, so it’s more important to focus on getting the architecture right, and achieving high test coverage
  • Adds strain to code reviews (eg. “don’t do it this way, I like this other way better”)
  • Even if I’ve been using a code style for a while, I still sometime have to pause and think about how to write a line better
  • Having an enforced, uniform code style makes it hard to experiment with potentially better styles
  • Maintaining a style guide takes a lot of incremental effort
  • Engineers rarely read through the style guide. More often, it's cited in code reviews

And as a secondary question: we also have many smaller repositories - should the same code style be enforced there?

© Programmers or respective owner

Related posts about architecture

Related posts about open-source