Standard Style, why not?
November 26th, 2008For most of our lives being slightly different is a good thing. We all have the same basic outline for clothes, but usually you are dressed differently than everyone else. Everyone has some variation of a car, but seeing a car identical to yours is not all that common. We desire to be unique, to have a slight variation on our existence. Unfortunately, this is not a good thing when it comes to writing code.
When given a choice, everyone is going to find a slightly different way of accomplishing the same things, as shown with the clothes and car example above. Code formatting is not an exception to this rule; everyone’s preferred formatting differs. Some put an open curly bracket on a new line, while others leave it on the same with a space after the closed parentheses, while others leave it on the same line without a space after the closed parentheses. Then there is the format that has class curly brackets on a new line, but function curly brackets on the same line. And you know there is some random guy out there that just has to be way different, who likes to line up their curly brackets in the middle of the name of the function it belongs to. Come on, this is stupid. It’s time to evolve and realize you’re not your fucking khakis…er, code formatting, you’re not your fucking code formatting.
I’m not the only one to think this way. Entry numero uno in Joel Spolsky’s The Best Software Writing I is on this very subject, written by Ken Arnold. He states,
For almost any mature language (C, Java, C++, Python, Lisp, Ada, FORTRAN, Smalltalk, sh, Javascript, ect.) coding style is an essentially solved problem, and we ought to stop worrying about it. And to stop worrying about it will require worrying about it a lot at first, because the only way to get from where we are to a place where we stop worrying about style is to enforce it as part of the language.
This is absolutely the way to go. Get all of us some nice code styling uniforms and make this a non-issue once and for all. I intend to devote a lot of time in this blog talking about small steps to being more productive. However, this would be a huge one for everybody. The fact that i can feel myself absorbing an entire block of code instantly when it is formatted in a way that is familiar to me, is huge. When it is an unfamiliar formatting, I end up having to read the code one line at a time, and that is just to get an idea of whether it’s the block of code that I need to be looking at in the first place. I believe it’s like any physical activity. Over time your body builds muscle memory for familiar tasks so that your brain doesn’t constantly need to be thinking every time you do it. Your brain delegates the tasks to the rest of the body. It’s the same with coding, and unfamiliar formatting makes it impossible for your brain not to be involved.
The best solution that I currently have for this is in Eclipse, other IDEs might also have this. Eclipse has a functionality that you can turn on that will properly format your code when you save or build. Your company can supply a couple of XML files that define exactly what the code formatting should be, and that’s it. Everyone writes code that is formatted exactly the same. This helps a lot to 1) guarantee people format their code the same way before checking it in to source control and 2) help new hires organically adjust to coding in the new style. Being a new hire myself, I don’t write code exactly to the specifications that the company expects. This functionality helps me to ease into the proper formatting and allows my brain to adjust gradually and without any extra thoughts dedicated to it. My code styling is evolving to match that of my new gig and my brain is evolving to the patterns it looks for in the code I’m working with.
I don’t see any reason why a compiler couldn’t have this functionality built-in. You try to compile source code that doesn’t match the required code style for the language, it auto-formats it for you…no errors at all. Easy as that. It’s something that truly needs to be considered as projects are just becoming too complex, and code style is just one more unneeded complexity.

