2020-10-14

Well, what format are we using now?

Code formatting guidelines are good. Code formatting guidelines that people follow are better.

Now, my team is pretty conciencious, and we all try to follow the guidelines, but there are two issues:

  1. We don't actually have a written standard.
  2. We're all humans. Reasonably attentive but rather fallible humans.

One solution to this is to adopt some kind of formatting tool and require that it be used regularly (or better yet, automate its use). Great. That's the plan. Only...

  1. Now we actually have to chose a standard.
  2. We already have roughly 120k SLOC written by the aforementioned fallible, moderately attentive humans and we want to minimize churn on the repository caused by simply adopting this tool.1

The key here is to select a standard that reflects what we've actually been doing with our slightly different personal takes on things, our mixed level of attention, and our occassional outright errors. But how do we look at nearly four hundred files and distill what we've actually been doing?

Right. We get a program to do it for us.

Better still someone else has already written it: whatstyle. This thing attempts to write an optimal style file for you by trial-and-error optimization with a metric like "fewest changes". Though exactly what that means is somewhat debatable.

It supports several common beautifiers including clang-format which is what my colleagues are using. It gave what looked like quite reasonable results when I ran it on a single large source file from my repository, so now I'm running over a large fraction of my repository at once (excluding a directory of legacy code we borrowed from another project and some "include this in your project" third-pary code).2 The only issue is that it took roughly a minute to do the one file (it is examining hundred to tens of thousands of style definitions in each optimization round), and now I've handed it more than three hundred files. For the moment my work laptop is functioning as a space heater.

My plan is to send the suggested format file to every coder involved in the project, ask for suggestions, and try to mediate the resulting chaos.

Wish me luck.


1 You're right. An experienced project leader would have adopted the fool thing at the beginning. I'll keep that in mind next time around.

2 Something roughly like this:

find ~/Projects/Foo -type f \
             -and \( -name '*.h' -or -name '*.cpp' \) \
             -and -not \( -name 'thirdpartycode.h' -or -path '*LegacyLibrary*' \) \
             -print0 | xargs -0 \
             whatstyle -f clang-format 

Then wait.

No comments:

Post a Comment