[Development] clang-format

resurrection at centrum.cz resurrection at centrum.cz
Tue Jul 3 20:13:52 CEST 2018


Interesting discussion about using clang-format Qt wide. As someone who implemented something like that in a project (of smaller size than Qt, but not all that small) some points:
 
- clang-format virtually annihilates A LOT of manual work. It might not seem like much but I have measured it and even had to work on two projects simultaneously where only one had clang-format. It was staggering how much time is spent on formatting when done manually. And how much it annoys you since you KNOW it can be done for you, automatically and always correctly. It also speed things up because if you make a formatting mistake it will be corrected as soon as you save the file in Qt Creator. So you don't bother correcting it, you just go on typing or hit ctrl+s if it annoys you to see it. You will get seriously addicted to that. It is really quite liberating.
 
- I have read many concerns here about loss of immediate history, that clang-format does not get it always right etc. Well we had those too but one thing beats them all - free consistency. I could hardly believe it but even though I did not like all of the rules we put in it the fact that they were consistent everywhere made reading the code far easier than before. You could not be surprised, you knew where will everything be. Whether it was includes and their ordering (the epic battles about that in pre-clang-format era...), alignment, wrapping, you name it. Once you could rely on the fact that it will be the same everywhere you suddenly did not have to focus on formatting which (again surprisingly) took a lot of code review time for all parties.
 
- Continuous integration. I cannot stress this one enough. Make it a standard in the repo, make everyone enable it in Qt Creator, make a git hook and make it part of the build. Our CI failed when someone committed incorrectly formatted file telling him as much even before compilation or anything. It happened rarely as most people used the git hook but still.
 
- Most people will be sceptical until they start working with it. I guess this is a given for any new technology or approach. I know I was. But I am yet to come by a colleague that would complain about it or who would want to remove it. It might not be perfect but the benefits far outweigh any issues.
 
And on more practical note:
 
- We converted all sources at once and it has not caused any problems - it's just formatting anyhow.
- Some files (very few) turned out to be somewhat broken but the reason for that was that they were curiously formatted in the first place. We fixed those as they were encountered. It generated negligible amount of extra "work" (as mentioned hitting Enter for new line and saving file to reformat typically was the trick).
 
I became a huge fan of clang-format thanks to making my work a lot more pleasant and less tedious experience. It might not seem like much but it really makes a big difference in daily work. I really hope it will be adopted Qt-wide.
 
My 2 cents and I would gladly answer any questions about practicalities as someone who did pretty much all of it from converting all sources to implementing the git hook and CI stuff.
 
Best wishes,


Michael Vlach
Software Engineer
NCR

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20180703/225881df/attachment.html>


More information about the Development mailing list