I'm currently working at a place that may be looking at forcing developers to use an automated code formatter on version control check-in. I'm looking for developers opinions on the advantages and disadvantages of doing this ... how you think it would help or hinder developers. My specific case involves Java/JSPs, but I think the question could apply to any language.
|
migrated from stackoverflow.com Jul 12 '11 at 13:57
|
I think it's very important to do this. Here's why:
If you do it, I would recommend everyone check all code in, then one person does a reformat over the whole code base, then checks it all back in so there's one "giant" change set for formatting (that everyone can ignore), but after that, all diffs are real code diffs. If you do it bit by bit, you'll be mixing real code changes with formatting changes and things will get unnecessarily messy in change land. |
|||||||||||||||||||
|
|
I'm going to toss my own answer here as people only seem to be adding advantages. What I see as disadvantages are:
Simply put, a non-automated set of conventions sets minimum style/readability requirements, where automated conventions set a minimum and a maximum. I remember looking at VB (version 5 maybe) and finding one of the most annoying things about it was that it would forcibly reformat my code and remove things above and beyond its basic formatting. |
|||||||||
|
|
I find that forced code formatting is great. It allows a developer to traverse the entire corpus of code without having their eyes bounce everywhere. Also having this standard in place helps novice developers break bad habits. |
|||
|
|
|
I've added an answer with disadvantages, and I'll throw in what I consider a big advantage as well. When you use an automated code reformat on commit, it does actually opens up the possibility of personal preference variations without the usual effect of having your preferences inflicted on others. You can have your IDE format code to a common standard on commit, but display it to you in your preferred format without affecting others. This to me is almost the Holy Grail of convention based coding ... you get the advantages of a common code format, but still allow personal preferences to be supported without conflicts. |
|||
|
|
|
Primary disadvantage is losing custom formatting where it really matters. Imagine a typical sanity check if() that will fail if any of the specific conditions is present but not fulfilled...
This is readable thanks to reasonable indenting following the logical structure of the conditions. Now your automated tool has no clue about logical separation of different conditions into related lines. It sees no reason why each clump 3-4 conditions in one line and split the next condition in half. Or it will split it, one comparison expression per line. It may even look prettier on screen but the logic will be lost. |
|||
|
|
|
It depends on your needs but some contraints are quite helpful, e.g. every if() should be followed by curly braces, since it's quite easy to get such an if wrong if you're refactoring. Consider that case:
If you now want to add some logging to the if case you might accidentially write:
And suddently your method always returns Edit: that's not on automatic formatting but rather style checking. Sorry if that answer was off topic as well. :) |
|||||
|
|
It greatly helps to uniform the code in the company, and thanks to that you generally produce a more easily understandable and way more easily maintainable structure for your product. |
|||
|
|
|
Well, the advantages are the same as any code formatter, like standardization of the code, semantic between developers, etc. The only possible disadvantages I see is the lack of a human eye after the formatting, to add some exceptions, etc. |
|||
|
|
|
In my experience, it's a good thing. Without one, code compares often show a mess of whitespace formatting, and may hide actual code changes. In my experience, messing with someone's formatting isn't the sin it's made out to be, especially with the potential benefits of consistency across the team. |
|||
|
|
