149

While I was reading this question, the top voted answer quoted Uncle Bob on coding standards, but I was confused by this tip:

  1. Don't write them down if you can avoid it. Rather, let the code be the way the standards are captured.

This bounced in my brain, but I couldn't find a place to stick. If a new person joins the team, or coding standards change, couldn't there be confusion of information?

Why shouldn't I write down a coding standard?

Thomas Owens
  • 82,739
Nolan Akash
  • 1,294
  • 49
    Hmm. I'm trying to imagine how this would work in a large multinational company with hundreds of developers and a code base spanning decades. – Matthew James Briggs May 24 '16 at 05:02
  • 31
    @MatthewJamesBriggs: it works as least as good as a written-down standard most of the devs ignore, or which had a different content at the time when the code was written initially. – Doc Brown May 24 '16 at 06:09
  • 7
    @MatthewJamesBriggs: agreed. The style guide should contain enough information to recognise previous conventions that are now deprecated, otherwise the culture of "copy the existing style" will find some 10-year-old horror to resurrect. Also, when cliques accidentally form that use different and incompatible specimens for deducing the official style, the written style guide says which of them is right (or ideally, prevents the clique forming in the first place). And if some of your hundreds of devs don't read docs, in a large company you can just fire them for gross muppetry. – Steve Jessop May 24 '16 at 08:50
  • 14
    Although to be fair, Bob also said you shouldn't have a company-wide coding standard in the first place, written or unwritten. Rather, each team/clique should develop its own. – Steve Jessop May 24 '16 at 09:07
  • 40
    Instead of writing a document that no one will read, use a linter that enforces code to be a certain way. If it's part of your development process, it's unavoidable. – Seiyria May 24 '16 at 12:45
  • 1
    @MatthewJamesBriggs Well, it's all about the culture. As long as you can keep the culture alive, it's going to carry over all of this "automagically". If you can't, the standards will likely be considered outdated and useless anyway. The real problem is when project management is so poor that people leave too fast to keep the culture, and no standards will help you then. – Luaan May 24 '16 at 14:21
  • 2
    @everyone I've been on the stackoverflow since January 2009 and never seen this "uncle bob" tag. Can someone explain what this "uncle bob" thing is (preferably by editing the "uncle bob" tag)? – Trevor Boyd Smith May 24 '16 at 14:42
  • 2
    To understand that comment from Uncle Bob, one must have a more broad understanding of that philosophy. Code should be "Self Documenting". It should read almost like a book. Commenting will ALMOST ALWAYS be left the way it is even after someone makes a change, leading to misunderstandings and code rot. Also, the Boy-scout Rule. Every time code is touched, always make it better. With these concepts (and others) in mind, his rule(s)/guidelines make sense. – Jeff.Clark May 24 '16 at 17:39
  • 1
    @Jeff Why? If I don't know the coding style I will have to spend lots of time reverse engineering all the details by looking at existing code. Before I've spent that investment my "improve everything I touch" will practically just make the code worse and inconsistent. – Voo May 24 '16 at 19:53
  • @Voo The point is that the code should read like a novel already, and that every time you touch your code, ask yourself things like, "Do these variable names make sense", or "with my additions is this function now too long--should split this function up into two functions?" Yes, if you are new to a company, you may not know if they use all caps for constants, but that is something you will need to look at and learn no matter what. When you figure that out, you can add that knowledge to your Boyscout rule repertoire. – Jeff.Clark May 24 '16 at 20:16
  • @Seiyria What would you suggest for more "high level" practices such as "how do we represent events in this code?" and "how should modules that use locking expose the API?" – Owen May 24 '16 at 22:16
  • @MatthewJamesBriggs - I work at that company, and I can describe it in one word: Badly. – Bob Jarvis - Слава Україні May 25 '16 at 03:19
  • 2
    @Jeff Whether variable names make sense or if a function is too long has little to do with a coding guide though (that's a generic enough to be obvious anyhow). The interesting things are "How do we do dependency injection in this code base?", "how are tests organized?" and so on. Nothing you can just divine without spending quite some time looking at the code. Now if it's neither an old nor large project that's fine. But a 15 year old multi million LOC application will have changed over time and it's impossible to update all existing code every time you learn how to do things better. – Voo May 25 '16 at 05:53
  • (Yes that would be nice, but is just infeasible). Hence you'll also have to look at the right parts of the code to figure out the current standards. Particularly if you have young developers, this won't be trivial for them. On the other hand giving them a few pages to read together with some high level overview over the different parts will be pretty helpful to get started. – Voo May 25 '16 at 05:57
  • 1
    I don't see code styles as different from other common company practices, like dress codes or parking regulations. If you want them followed you need to develop a culture of conforming to expectations and you need to write them down. Granted, even if you do both of those things people might ignore the rules, but if you only do one or zero of them then there will definitely be significant groups of people ignoring the rules. I'm not sure how big a group can be before it's useful to write down the important decisions you've made, but speaking for myself I think 1 might be too big to skip that. – Steve Jessop May 25 '16 at 09:49
  • @TrevorBoydSmith: tag edited, done. – Doc Brown May 26 '16 at 08:50
  • Figure out how to encode them in the formatter for the IDE used for the team, and ensure everybody use them when formatting. – Thorbjørn Ravn Andersen May 27 '16 at 08:15
  • However coding standard is not merely about formatting... – Adriano Repetti May 27 '16 at 19:30
  • @SteveJessop - I think the difference between code styles and dress codes are that there are tools that make conformance easier. Sonar and CheckStyle are the way to address this stuff now-a-days. So Uncle Bob's comment probably has to be taken in light of the fact that "writing them down" is still kind of pointless (nobody cares or reads them), but implementing them into your build pipeline is not pointless. The cultural aspect that Uncle Bob's seeking is handled in how the team prunes the rules in the tool. So really, the whole argument is kind of an anachronism these days. – Calphool May 28 '16 at 17:44
  • Why that obsession with your uncle? I'm not really interested in your family affairs. You have been posting about your uncle whatshisname repeatedly. – gnasher729 Feb 28 '18 at 08:07

14 Answers14

259

There are a few reasons.

  1. Nobody reads documentation.
  2. Nobody follows the documentation even if they do read it.
  3. Nobody updates the documentation even if they do read it and follow it.
  4. Writing a list of practices is much less effective than creating a culture.

Coding standards are not about what people should do, but are about what they actually do. When people deviate from the standards, this should be picked up and changed through a code review process and/or automated tools.

Remember, the whole point of coding standards is to make our lives easier. They're a shortcut for our brain so that we can filter out the necessary stuff from the important stuff. It's much better to create a culture of review to enforce this than it is to formalise it in a document.

Stephen
  • 8,848
  • 3
  • 30
  • 43
  • 4
    Comments are not for extended discussion; this conversation has been moved to chat. – maple_shaft May 24 '16 at 14:41
  • 12
    And if you want to enforce things, you should have a build process step that enforces it, not a style guide that does. – Wayne Werner May 24 '16 at 18:35
  • 34
    Do you really not have a standard document, but just yell at people for the first few weeks at every code review? This seems like you'd basically expect beginners to reverse engineer your coding style guides. Or is this more a hypothetical "I think that's what he meant"? Now if this is about automatic tools enforcing those rules - agreed, that's essential. But I don't want to have to laboriously figure out the rules. – Voo May 24 '16 at 19:44
  • 13
    @Voo, why do you feel you need to yell during a code review if an issue comes up? – Jaap May 24 '16 at 20:39
  • 2
  • If they're not written down anywhere, rules pedants can't point out that your code hasn't technically been following the standards for months and should all be revised even though it's working fine.
  • – aroth May 25 '16 at 04:24
  • 20
    @Jaap I thought the hyperbole was obvious.. apparently not. No not yelling at people, but telling them that they have to go back and fix all the things they violated because they couldn't know better. – Voo May 25 '16 at 05:49
  • 5
    @Voo: you don’t need to “reverse engineer coding style guides”. The conventions should be apparent when looking at the existing code. Everything which doesn’t immediately jump into the eye is either, uncommon or not even fixed. In case there are really important rules about something rarely occurring, it might be worth discussing it with the new developers, when they are the ones who have to write such code. Communication can’t be overestimated. – Holger May 25 '16 at 12:14
  • 1
    @Voo I love having my code reviewed. It forces me to write better code and justify my decisions to someone who is viewing it without prejudice. I've never been yelled at or even aggressively attacked during a code review. – Stephen May 26 '16 at 00:32
  • 1
    @maple_shaft has already moved long conversations to chat once. does he have to do it again? – Insane May 30 '16 at 02:45