My recent post about Google’s C++ style guide attracted some great comments. Billy O’Neal corrected my ignorance about C++ formatting tools by alerting me to the existence of clang-format; John Payson came up with a way of getting piecemeal from the “no exceptions allowed” state to the “exceptions allowed and all code able to handle them” state, and Titus Winters (a maintainer of the Google style guide) mentioned a talk he gave at CppCon on the subject.
The talk is on YouTube here, and is worth watching in its entirety, but here are a few highlights I took away from it:
|1:42||“How should we format our code? BORING QUESTION!”. Titus introduces clang-format.|
|3:14||What is the purpose of a style guide?|
|5:15||Google’s publicly available style guide is “probably optimizing for a different set of values than your organization needs”|
|6:48||Google has an indexer – Kythe – which should be available as open source soon.|
|9:39||Google plan to be around for at least 10 years. Almost everywhere I have worked has assumed that they are going to be around for 10 years (and several of those places went bust a long time short of 10 years), but very few of my employers have made long term decisions about their code – they might have assumed that they will be around for 10 years but on the code side they certainly didn’t plan for it.|
|10:55||The philosophies of the style guide.|
|14:45||A future version of the style guide will allow streams.|
|28:10||“No non-const references as function arguments” is one of the most controversial rules in the style guide. I am not surprised by this, but I am disappointed. The arguments in favour of no non-const references always seemed good to me.|
|38:25||“I don’t care about throwing out of memory exceptions”. There are certainly products where an out of memory exception just translates to “die instantly”, however that isn’t universally true. I have never worked on a project that was able to recover and continue after running out of memory but I have worked on products that have wanted to know about the out of memory case so they can save the data the user is working on. It’s just another reminder that guidelines depend on the products they are being used for.|
|57:20||Probably the best answer to any formatting question that I have heard.Q: What about tabs vs. spaces? What’s Google’s take on that?A: I don’t care … I just use clang-format|
The theme of the day is clang-format. I spent some time over the weekend playing around with clang-format and it is excellent. I haven’t worked out how to get it to do quite everything I want, however the consistency benefits far outweigh any minor deviations from my preferred style. Also, if it bothers me that much I have the source code and can add more options.
On a related note, this is the first time I have been able to get Clang and LLVM (mostly) compiling on Windows without spending a ridiculous amount of time on it. Windows has long been the afterthought platform for LLVM and Clang – it looks like it’s getting better.