Monday, November 13, 2006

The Importance of Code Formatting

I'm currently working through the mess of code. It's not fun. The code is poorly written. It has no consistency in the spacing, variable names, comment style, or name conventions. There's whole pages of dead code and comments attached to nothing.

How did the code end up this way? As far as I can tell, I am the fifth or sixth person to work on this code base. Naturally, there's a small amount of dissimilarity in styles to be expected. That's a speedbump in productivity, but no big deal. But, the person before me to work on the code clearly had no idea what he was doing. The formatting of this code is all over the place. And now, the consequence is that the code is difficult to read and understand. I've discovered several bugs that are due to extra and missing brackets. From bad names and extreaneous comments, the only way to figure out how parts of the program work is to step through line by line and deduce the logic. All of that adds up to a lot of wasted time on my part.

So, how does one evade this problem? I believe that there are two solutions that are needed.

Firstly, there is a need for better tools to do code formatting. Everytime code is checked in to a repository, in must be beautified. Unfortunately, I have yet to find a tool that does a satisfactory job. But there are a few candidates that may work after a few feature additions.

Secondly, a code and documentation style guide should be used. Had I insisted on the use of a style guide early in the project, a good chunk of this mess could have been avoided. And, a printed version of a guide is handy too. When a programmer stops following it, it can be rolled up and smacked accross his nose.

I need to do more work on both these items before they can be put into practice. Hopefully, I'll come up with something that can make my future collaborative programming a saner experience.

No comments: