Nelz's Blog

Mah blogginess

How to Run a Code-Formatter

Code Formatting

This can be a very charged subject. I am going to explain my reasons and thought process. Your mileage may vary.

Up until recently, we did not have any specified "code style" defined. Then, J. shot from the hip and said, "Use Sun Standard code style." That was a step in the right direction, but was still kinda wishy-washy. Since we’re never given time for code reviews, nor is there any one developer who actually codes to the Sun style, I didn’t see how that was going to be all that productive.

Then I realized what was missing: visibility. Until we saw a certain style, consistently implemented on a consistent basis, not coding style would ever actually take root. How does one give one’s code a homogenous style? Code formatters!

So, there are about a zillion and one ways to implement a code formatter, so I’ll cover the options that I thought might be viable.

First, we could just delegate it to the developers themselves. Get the Jalopy plugin, use the same ruleset, and when you are in the code, remember to format it. There’s two big flaws here: 1) the developer has to remember to format every single piece of code that he touches, and 2) you end up co-minling logic changes and style changes into single change check-ins. (If you’ve ever had to do version-to-version comparison between styled and un-styled code, you already know how difficult it is to find the actual logic change amongst all the other changes.)

Second, we could have set up some kind of "macro" with our version control system to apply the style upon checkin of any code. This gives me the willies, thougth. You still have the co-mingled logic+style change problem, and in addition, what gets checked in is not necessarily what the programmer has at his desk. Ugh!

My last thought was the winner: Enable some type of script to actually go through all the code, applying just the style changes, and check in those changes as their own atomic unit. This works especially well if you can orchestrate a time when no one has any code out. (The end of a sprint, after everything is delivered?)

We use Perforce for SCM, and ANT for our builds. I was able to use the ANT/Perforce tools on our CruiseControl server to check out all the files, run the formatter, revert any un-changed files, and check in the modified files, all from one on-demand CruiseControl project.

(As of the previous post, I have included the ANT tasks from the ANT delegate-build for performing this process.)

I hope this helps!