Skip to content

coding convention: break brace after multiline control statement#22205

Draft
maribu wants to merge 3 commits intoRIOT-OS:masterfrom
maribu:clang-format/break-brace-after-multiline-control-statement
Draft

coding convention: break brace after multiline control statement#22205
maribu wants to merge 3 commits intoRIOT-OS:masterfrom
maribu:clang-format/break-brace-after-multiline-control-statement

Conversation

@maribu
Copy link
Copy Markdown
Member

@maribu maribu commented Apr 20, 2026

Contribution description

We current do not wrap the brace after a control statement ever. This changes the behavior to still not break the line before the brace by default, but do break it on multiline statements.

E.g. before:

if (boolean_expr1) {
    do_something();
}

if (boolean_expr2 &&
    boolean_expr3) {
    do_something();
}

With this change this now becomes:

if (boolean_expr1) {
    do_something();
}

if (boolean_expr2 &&
    boolean_expr3)
{
    do_something();
}

Note that the behavior before suffered from poor readability of the code structure, as the indent does not make it obvious where the condition of the if statements ends and where the function body starts. With this change, the code becomes one line longer for the rare long boolean expressions, but the code structure is much easier to spot.

Testing procedure

clang-format will not remove the line break in front of { if and only if the condition is split over multiple lines.

Sadly, clang-format will not add the line breaks ever unless a maximum line width is specified. But clang-format is relatively heavy-handing in filling lines up until the column limit, which doesn't match with the soft and hard column limit defined in our coding conventions. Hence, reflowing content has been disabled in clang-format so far.

Issues/PRs references

None

Declaration of AI-Tools / LLMs usage:

AI-Tools / LLMs that were used are:

  • none

PR State

  • update clang-format to match the updated coding convention
  • update uncrustify to match the updated coding convention

maribu added 2 commits April 20, 2026 21:14
Adding a line break before the `{` after a multiline if statement helps
readability a lot, as telling apart where the function body starts and
where the if conditions ends becomes trivial from the indent only.
We current do not wrap the brace after a control statement ever. This
changes the behavior to still not break the line before the brace by
default, but do break it on multiline statements.

E.g. before:

```c
if (boolean_expr1) {
    do_something();
}

if (boolean_expr2 &&
    boolean_expr3) {
    do_something();
}
```

With this change this now becomes:

```c
if (boolean_expr1) {
    do_something();
}

if (boolean_expr2 &&
    boolean_expr3)
{
    do_something();
}
```

Note that the behavior before suffered from poor readability of the
code structure, as the indent does not make it obvious where the
condition of the if statements ends and where the function body starts.
With this change, the code becomes one line longer for the rare long
boolean expressions, but the code structure is much easier to spot.
@github-actions github-actions Bot added the Area: doc Area: Documentation label Apr 20, 2026
@crasbe crasbe added the Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation label Apr 20, 2026
Comment thread CODING_CONVENTIONS.md
…tmts

Co-authored-by: crasbe <crasbe@gmail.com>
@kfessel
Copy link
Copy Markdown
Contributor

kfessel commented Apr 27, 2026

WTF why worsen (more special handling) a already ugly coding style

is the another well known project that does that?

we could instead just remove the requirement to have the { in the same line as the if
-> less rules less exceptions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: doc Area: Documentation Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants