How to Format Your Code
We use clang with autoformatting and code-completion to help us normalize our codes. This improves overall code readability not only for you, but for others reading your code.
CLANG-FORMAT
Make sure to have it in the root of your repository, formatters look from the inside-out in the current directory for the format specifications, so that it is applied to all your codes.
clang-format can be easily integrated into your IDE (e.g., VSCode, Vim, etc.) to automatically format your code on save or on demand. This ensures that all your code adheres to the same style guidelines without requiring manual effort.
Clang-Format Configuration
The .clang-format file with our formatting looks like this:
.clang-format
.clang-format file:
Language: Cpp
Standard: c++20
AccessModifierOffset: -2
ColumnLimit: 160
MaxEmptyLinesToKeep: 2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakTemplateDeclarations: true
BreakBeforeBinaryOperators: false
BreakBeforeBraces: Custom
BreakConstructorInitializers: BeforeColon
KeepEmptyLinesAtTheStartOfBlocks: true
NamespaceIndentation: None
SortIncludes: false
SpaceBeforeParens: ControlStatements
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: true
AfterFunction: false
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: false
IndentBraces: false