How to Get Coding Standards Right (and Wrong)
If you must have coding standards, you need to automate them as much as possible. Automating them provides so many wins and inoculates against some of the traps.
Join the DZone community and get the full member experience.Join For Free
nothing compares with the first week on a new job or team. you experience an interesting swirl of anticipation, excitement, novelty, nervousness, and probably various other emotions i’m forgetting. what will your new life be like? how can you impress your teammates? where do you get a cup of coffee around here?
if you write code for a living, you know some specific new job peculiarities. do they have a machine with runnable code ready on day one? or do you have to go through some protracted onboarding process before you can even look at the code? speaking of code, does theirs square with elegant use of design patterns and unit testing that they advertised during the interview process, or does it look like someone made a death star out of bailing wire and glue?
one of the most pivotal moments (for me, anyway) comes innocuously enough. it usually happens with an offhand comment from a senior developer or through something mentioned in your orientation packet. you find yourself directed to the coding standards document. oh, boy.
at this point, i start to wonder. will i find myself glancing at a one-pager that says, “follow the microsoft guidelines whenever possible and only include one class per file?” or, will i find something far more sinister? images of a power-mad architect with a gleam in his eye and a convoluted variable name encoding scheme in his back pocket pop into my head. will i, therefore, spend the next six months waging pitched battles over the placement of underscores?
ugh, coding standards
in this post, believe it or not, i’m going to make the case for coding standards. before i do so, i want to make my skepticism very clear. accordingly, i want to talk first about how coding standards fail.
based on personal battle scars and my own experience, i tend to judge coding standard documents as guilty until proven innocent. i cannot tell you how many groups i have encountered where a coding standard was drafted, “just because.” in fact, i’ve even written about this in the past .
the morale trap
probably the most obvious failure scenario that i see is the “coding standards as cudgel” one. in this situation, a strong personality or an “in-crowd” within the team uses the standards documents to bash everyone else into submission. the standard becomes not a means to any end, but an end unto itself.
when this happens, it can have a brutal effect on the team’s productivity. friction and contentiousness mount. morale for members of the team tanks. people feel that their jobs are overly-regimented and lack any need for creativity, leading to behaviors associated with learned helplessness. no amount of code readability can make up for this.
the cargo cult trap
part and parcel with learned helplessness become what i think of as “the cargo cult trap.” with an overzealously created, massive standards document, the team comes to worship at the altar of the process. the standards document provides the illusion of guidance in all situations and gives the team an inappropriate sense of air cover.
oh, there’s a bug in my code, huh? well, that’s unfortunate, but i did follow the standard, so i guess there was really no avoiding it.
obviously (hopefully), nobody would be quite that obtuse, but you get the idea. when you have prescriptive guidelines for every conceivable scenario, successfully following all of those guidelines becomes the team’s main charter and challenge. this holds doubly true if the response to every bug is to mark up the standards document so it never happens again.
the busywork trap
the final (and perhaps least insidious) trap that i’ll mention is the busywork trap. unlike the morale problems and outsized trust in the standards, this trap offers minimal outright damage. instead, you have simple waste. this also probably occurs most commonly.
in this scenario, the coding standard is created and maintained, but ignored. or perhaps the team does not entirely ignore it, but only selectively applies it or else only some of the team pays attention. another, similar, situation occurs when the team lays out standards and guidance for rare or nonexistent situations.
i mention this trap because i’ve found it to be the most common. the team makes a good faith effort at standardizing, only to have that work largely go to waste.
it works where it solves a problem
true to my word, i’ve certainly hammered on the “guilty until proven innocent” theme. i do this because i believe that prescriptive instruction (like coding standards) should happen not by default, but only to address a gap or shortcoming.
has the team consistently introduced bugs and wasted troubleshooting time because half of the team uses an underscore prefix to mean one thing and half another? do newer members of the team consistently make a correctness or performance mistake in the code? have folks consistently introduced dependencies between projects that create deployment problems?
if you have situations like these, then standardizing helps. let a standard evolve out of need; you shouldn’t adopt one as a preventative measure.
build a system
that brings me to my final point. i’d also argue that this may serve as the differentiator between my thoughts and those of others writing on the topic. if you must have coding standards, you need to automate them as much as possible. automating them provides so many wins and inoculates against some of the traps.
a colleague once told me that it’s best to break by bad news by building a system and blaming things on the system. as a consultant, this has proved immensely valuable to me, and it applies well in the domain of coding standards.
coding standards in code, not word
if you really can’t stand camel case or brackets on new lines or whatever, don’t send nasty emails or get into arguments during code review. don’t keep the standard in a word document. instead, add something to your build that fails or prevents check-ins of code that fails to comply. then it’s no one on the team holding judgment, but the tool.
this avoids contentiousness and it allows for a tight feedback loop. the team learns and adopts new norms quickly. it also tends to put to bed (to some extent) religious wars about the standard via a manifestation of squatter’s rights.
what’s more is that automating the standard requires effort, so it tends to put a damper on cargo-cult standard adherence and busywork both. (within reason.) you’ve really got to want it to go into the build machine and add automation to boot a build back to the committing developer. that’s harder than sending an email or updating a document.
i mention this on the ndepend blog because i find ndepend to be invaluable in creating systems like this. it can effortlessly transcend considerations that are both cosmetic, like naming, and design-centric, such as rules about semantics and architecture. not to mention that its cqlinq makes the possibilities virtually limitless.
ndepend is, in my opinion, the best tool for the job. whatever tool you use, be sure to use a tool. coding standards can help you eliminate repeat mistakes and create more maintainable code. make sure that the medicine isn’t worse than the disease — make sure you automate.
Published at DZone with permission of , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.