← Today I Learned

Prevent ClangFormat from breaking before curly braces

For some unfathomable reason, C and C++ developers sometimes put line breaks before curly braces:

int main()
{
  for (int i = 0; i < 10; i++)
  {
    printf("%d\n", i);
  }
}

It’s also the default formatting style in the code formatter ClangFormat.

Fortunately, it’s configurable by setting the BreakBeforeBraces option to Attach in your .clang-format file:

BreakBeforeBraces: Attach

Much more readable:

int main() {
  for (int i = 0; i < 10; i++) {
    printf("%d\n", i);
  }
}