Marc Mutz (16 July 2026 11:14) wrote:
> 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 generally deal with that by just increasing the indentation of the
continuation lines of the condition,

if (address.isEmpty() || !isValid()
        || !codec) {
    return false;
}

although, of course, the policy of putting the operator at the start of
the continuation line (of which I'll admit to not being a fan) does
mitigate the problem quite a bit, in its own right, simply because
that's an unusual way for a non-condition line to start.

I'm fairly sure Thiago has asked for such extra indent on continuations
of conditions in the past.

Of course, for very long last lines of a condition, that might hit the
right margin, but there's other ways to deal with that.

> 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 have used this in the past weeks here and there, either mentioned in
> the commit message or silently, and I didn't receive any push-back.

Can't say I'm a fan.
I would probably have objected if I'd been in one of those reviews, but
maybe I was and missed it.

I do separate an open-{ from the end of a condition when there's #if-ery
involved and doing so avoids having one of it and its close-} inside
(and thus duplicated across) the #if-ery and the other not, but that's
not the case here.

> NB: this does _not_ mean that an else should use this format
>
>  if (address.isEmpty() || !isValid()
>      || !codec)
>  {
>      return false;
>  }
>  else // Wrong!
>  {
>  }

If we go ahead with your change, this looks *less wrong* to me than what
you're proposing:

>  if (address.isEmpty() || !isValid()
>      || !codec)
>  {
>      return false;
>  } else { // Correct
>  }
>
> ¹ https://wiki.qt.io/Qt_Coding_Style#Braces Bullets #1 and #6.

I find the asymmetry jarring.

        Eddy.
-- 
Development mailing list
[email protected]
https://lists.qt-project.org/listinfo/development

Reply via email to