[Development] Suggesting update brace placement following a multi-line if condition
André Hartmann
aha_1980 at gmx.de
Sat Jul 18 10:36:34 CEST 2026
Hi Elvis,
this is exactly the style I use in my personal projects, and just as
you, I find this very consistent.
But I uses it sparely, because such long if conditions are always a sign
that something is overcomplicated. I prefer to use helper variables that
are assigned before the if-condition, which almost always increases the
readability and also helps to debug the code.
Regards, André
Am 18.07.26 um 10:12 schrieb Elvis Stansvik:
> Den fre 17 juli 2026 17:28André Somers via Development <development at qt-
> project.org <mailto:development at qt-project.org>> skrev:
>
> __
>
> Hi,
>
>
> On 16-07-2026 11:14, Marc Mutz via Development wrote:
>> Hi,
>>
>> The current style guide¹ asks for attached braces on if
>> statements, incl. when the if condition spans multiple lines (#6):
>> // Correct if (address.isEmpty() || !isValid() || !codec) { return
>> false; }
>> I've been bothered by this for years, since I think this is
>> totally unreadable. In particular, if the last line of the if is
>> on the longer side, the body of the then branch is almost
>> impossible to distinguish from a continuation of the condition.
>>
>> I would therefore like to propose to allow placing the opening
>> brace on a separate line if the if condition is multi-line:
>> // (now also) Correct if (address.isEmpty() || !isValid() || !
>> codec) { return false; }
>
> I agree. I find this more readable. But I would prefer to have
> logical groups of the condition be either broken one per line, or
> all on the same line. So:
>
>
> // Correct
> if (address.isEmpty() || !isValid() || !codec) {
> return false;
> }
>
> or
>
>
> // (now also) Correct
> if (address.isEmpty()
> || !isValid()
> || !codec)
> {
> return false;
> }
>
> If we're going to have a bit of suggestion bingo in here, I have to
> throw in:
>
> if (
> address.isEmpty()
> || !isValid()
> || !codec
> ) {
> return false;
> }
>
> I.e. not only putting each group on its own line, but also letting it be
> there by itself without any (, ) or {. I think that lets you focus on
> what the group is saying best, without any "distraction" on the line.
>
> Realize some people may not like this "tall" style, but thought I'd
> throw it in there since it somewhat sneakily dodges the question of the
> opening brace - it's consistent with other situations in that it's kept
> with the ), but there's a bit of "air" between conditions and body.
>
> But also this:
>
> // (now also) Correct
> if ((address.isEmpty() && !isValid())
> || !codec)
> {
> return false;
> }
>
>
> Cheers,
>
>
> André
>
>
> --
> Development mailing list
> Development at qt-project.org <mailto:Development at qt-project.org>
> https://lists.qt-project.org/listinfo/development <https://lists.qt-
> project.org/listinfo/development>
>
>
More information about the Development
mailing list