Brace Style Comparison: K&R vs Allman vs GNU | Web Formatter Blog

Introduction to Brace Styles
Brace styles define how curly braces and indentation are formatted in code. While they don't affect how code executes, they significantly impact readability and maintainability. This article compares the most popular brace styles used across programming languages.
The placement of braces and indentation patterns are often hotly debated among developers. Each style has its advantages and historical context, and many languages have adopted specific conventions that have become standard in their communities.
K&R Style (Kernighan & Ritchie)
The K&R style, named after Brian Kernighan and Dennis Ritchie (creators of C), is one of the most widely used brace styles. It places opening braces on the same line as the control statement and closing braces aligned with the control statement.
if (condition) {
statement1;
statement2;
}
else {
statement3;
statement4;
}
Advantages:
- Compact vertical space usage
- Widely adopted in C, JavaScript, and Java
- Default style in many formatters
Disadvantages:
- Some argue it makes code blocks less visually distinct
- Can be harder to match opening and closing braces in complex nested structures
Allman Style
The Allman style, named after Eric Allman, places opening braces on a new line, aligned with the control statement. This creates a clear visual block that some find easier to read.
if (condition)
{
statement1;
statement2;
}
else
{
statement3;
statement4;
}
Advantages:
- Clear visual blocks make code structure more apparent
- Easier to identify block beginnings and endings
- Popular in C#, C++, and other languages
Disadvantages:
- Uses more vertical space
- Can make short functions appear unnecessarily long
GNU Style
The GNU style is a variation of the Allman style with additional indentation for the braces. It's used in GNU projects and emphasizes the hierarchical structure of code blocks.
if (condition)
{
statement1;
statement2;
}
else
{
statement3;
statement4;
}
Advantages:
- Emphasizes block hierarchy
- Consistent with GNU coding standards
- Makes nested blocks more visually distinct
Disadvantages:
- Uses even more vertical space than Allman
- Less common outside GNU projects
- Can lead to excessive indentation in deeply nested structures
1TBS (One True Brace Style)
The One True Brace Style is a variation of K&R that requires an opening brace for all control structures, even those with a single statement. This helps prevent bugs when statements are added later.
if (condition) {
statement1;
} else {
statement2;
}
for (i = 0; i < 10; i++) {
singleStatement();
}
Advantages:
- Prevents bugs from missing braces when adding statements
- Consistent application of braces throughout code
- Popular in JavaScript and modern web development
Disadvantages:
- Can make simple control structures appear more complex
- Uses more vertical space for single-statement blocks
Stroustrup Style
The Stroustrup style, named after Bjarne Stroustrup (creator of C++), is a variation of K&R where functions have opening braces on a new line, but control statements have opening braces on the same line.
int function()
{
if (condition) {
statement1;
statement2;
} else {
statement3;
}
return 0;
}
Advantages:
- Distinguishes functions from control structures
- Balances vertical space usage
- Popular in C++ communities
Disadvantages:
- Inconsistent brace placement can be confusing to newcomers
- Less common in other language communities
Whitesmiths Style
The Whitesmiths style places both opening and closing braces aligned with the indentation level of the contained code, creating a unique visual pattern.
if (condition)
{
statement1;
statement2;
}
else
{
statement3;
statement4;
}
Advantages:
- Braces align with their content
- Creates a unique visual pattern that some find intuitive
Disadvantages:
- Uncommon in most programming communities
- Can be confusing to those used to other styles
- Not well-supported by default in many formatters
Horstmann Style
The Horstmann style, named after Cay Horstmann, places opening braces at the end of the line but indents them to the same level as the contained statements.
if (condition) {
statement1;
statement2;
}
else {
statement3;
statement4;
}
Advantages:
- Emphasizes the connection between braces and their content
- Creates clear visual blocks
Disadvantages:
- Rare in practice
- Unusual indentation can be confusing
- Poorly supported by automatic formatters
Language-Specific Conventions
Many programming languages have developed their own conventions for brace styles:
- JavaScript: Predominantly K&R style, with ESLint and Prettier defaults supporting this
- Java: Primarily K&R style as recommended by Oracle's style guide
- C#: Microsoft recommends Allman style in their C# coding conventions
- Python: Uses indentation instead of braces, but the indentation pattern resembles Allman
- PHP: PSR-12 standard recommends K&R style
- Ruby: Uses minimal braces, but when used, follows K&R style
- Go: Enforces K&R style through gofmt
- Rust: Uses K&R style by default in rustfmt
Formatter Configuration
Most code formatters allow you to configure brace styles. Here are examples for popular formatters:
Prettier (JavaScript, TypeScript)
Prettier enforces K&R style and doesn't offer configuration for brace style.
ESLint (JavaScript)
{
"rules": {
"brace-style": ["error", "1tbs", { "allowSingleLine": true }]
}
}
ClangFormat (C, C++)
BreakBeforeBraces: Allman # Options: Attach (K&R), Allman, GNU, Stroustrup, etc.
EditorConfig
[*.{c,cpp,h,hpp}]
indent_style = space
indent_size = 4
# Note: EditorConfig doesn't directly control brace style
Team Consistency
Regardless of which style you prefer, consistency within a project or team is crucial. Here are some tips for maintaining consistent brace styles:
- Document your chosen style in a coding standards document
- Use automated formatters in your CI/CD pipeline
- Configure editor settings to apply the correct style on save
- Use pre-commit hooks to enforce style before code is committed
- When working on existing projects, follow the established style
Conclusion
Brace style is ultimately a matter of preference and team convention. While debates about the "best" style continue, the most important factor is consistency within a codebase. Modern code formatters make it easy to enforce a consistent style across your projects.
When choosing a brace style for your project, consider: - Language conventions and community standards - Team preferences and existing codebases - Readability for your specific use cases - Available formatter support
Remember that the best brace style is the one that your team agrees on and consistently applies throughout your codebase.
Related Topics:
Try Our Formatting Tools
Format your code with our free online tools:
Need more formatting tools?
Explore our complete collection of free online code formatting tools.
Browse All Formatters