Alphabetical ordering

Sept. 20, 2015, 12:18 p.m.

Where I worked this summer, there was a rule to always alphabetize related lines before sending the changes off to a code review.

This is bad:

  doVenus()
+ doMercury()
+ doMars()

This is good:

+ doMars()
+ doMercury()
  doVenus()

I would agree wholeheartedly if that's simply the code style of the project, in same the way that public methods should go before private methods, etc.

However, the first reasoning I was given was that alphabetizing changes reduces conflicts in a three-way merge. Certainly this has nothing to do with the diff algorithm that git uses (usually Myers, which deals with longest common subsequences, not text content). The intuition is probably that alphabetizing provides the opportunity to spread additions throughout the file, rather than confining them to the end where every change is a conflict. Then the question is, would alphabetizing still be better than random placement of lines? Intuitively, probably so, but I don't see any studies measuring this. For now, I'll chalk it up as "possibly useful voodoo", like most software engineering techniques are. ;)