Code Formatter

I used to think formatters are bad, because you would jump on a codebase, and then autoformat everything. But just like how there are Layers of Truth to chocolate

But turns out that what you should do instead is just make sure to follow the codebase’s style guide, by using the .clang-format file.

Big bug

On mac, I was using the latest version of clang-format, and it just wasn’t formatting correctly for some reason. Used clang-format-11 and it was fixed.

C++

I like Google’s style formatter, Chester uses Chromium for some reason.

The most popular C++ formatter is Clang.

.clang-format file:

BasedOnStyle: Google
ColumnLimit: 0
IndentWidth: 4
InsertNewlineAtEOF: true
InsertBraces: true

Google’s style guide uses 2 spaces for a tab, because nested functions have a lot of indents apparently, and that’s the reason.

How to do this with VScode?

  • Might need the Clang-Format Extension

  • So the behavior I want is that by default, it doesn’t format anything if it doesn’t detect a .clang-format file. You can do this by setting none inside Clang_format_fallback Style.

Other

clang-tidy

this is a static analysis tool, Kajanan showed it to me

ROS2

ROS2 uses the Google C++ Style guide + some modifications. https://docs.ros.org/en/humble/The-ROS2-Project/Contributing/Code-Style-Language-Versions.html#id3