From 23b677ab28ae0fe2c545033e83d69b7fbf0af009 Mon Sep 17 00:00:00 2001 From: Jake Lazaroff Date: Fri, 22 Dec 2023 17:30:45 -0500 Subject: [PATCH] Prevent ClangFormat From Breaking Before Curly Braces --- ...ormat-from-breaking-before-curly-braces.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 c/prevent-clangformat-from-breaking-before-curly-braces.md diff --git a/c/prevent-clangformat-from-breaking-before-curly-braces.md b/c/prevent-clangformat-from-breaking-before-curly-braces.md new file mode 100644 index 0000000..413a9fb --- /dev/null +++ b/c/prevent-clangformat-from-breaking-before-curly-braces.md @@ -0,0 +1,31 @@ +# Prevent ClangFormat From Breaking Before Curly Braces + +For some unfathomable reason, C and C++ developers sometimes put line breaks before curly braces: + +```c +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](https://clang.llvm.org/docs/ClangFormat.html). + +Fortunately, it's configurable by setting the [`BreakBeforeBraces` option](https://clang.llvm.org/docs/ClangFormatStyleOptions.html#breakbeforebraces) to `Attach` in your `.clang-format` file: + +``` +BreakBeforeBraces: Attach +``` + +Much more readable: + +```c +int main() { + for (int i = 0; i < 10; i++) { + printf("%d\n", i); + } +} +```