Thursday, February 10, 2011

Don't reformat code....

So, the old brackets argument....

if (x==0) {
    // Do Something here


if (x==0) 
    // Do Something here

Well here's the answer. They are both fine. Leave 'em  be.

And here's why.

You will find some tools or editors or plugins that will reformat your code for you.  Make it nice and pretty, so you don't have your sensibilities jarred by misplaced brackets.

There's a catch. There often is.

What usually happens is the source code control system finds a common root for your file and the 
file on the other branch that you want to merge with. Then  it looks at the changes between the root and your file and the changes between the root and the other file.

In many many cases, it can merge the code without the user even looking. If it can't it simply reports the conflicts. I have often done merges of hundred of files, where less than a dozen had conflicts.

However, Source Code Control Systems typically use text based file compares for code merges. And while these can deal with missing white spaces, or extra blank lines, the simple act of moving the bracket from one line to another confuses the 3 way file comparison.

So, when you merge your branch with some other branch, and you have run a code reformatter across dozens of source files, if only your file has changed, no problem. 

At least, no problems for now.

A little while later, someone else tries to merge their code. And they find that in every case where they have a change, you also have a change. A bracket here, re-ordering using statements there, ordering the methods into the same order as the interface perhaps. The screams of anguish as they realise that they will have to manually merge every single file, and the chaos of colours marking the adds, deletes and changes in the three way merge will burn into your soul. 

You will never do this again. 

No comments:

Post a Comment