Re: Ternary operator formatting

Christopher Smith <[email protected]> Mon, 27 Jul 2026 12:47:09 -0500
Newsgroups gmane.comp.lang.groovy.devel
Message-ID <CAG+8NVqgMw=3U8Do8pZjogZm98UqibvPhRUnEhDCz6ranvz9jA@mail.gmail.com>
--0000000000004c55ac06579b5049
Content-Type: text/plain; charset="UTF-8"

That's exactly how we use them: One-liners for short expressions and
stacked for long ones, especially GStrings. Highly context-specific.

Christopher Smith

On Mon, Jul 27, 2026, 12:03 MG <mgbiz-yvYIh6MZAuFWk0Htik3J/[email protected]> wrote:

> Hi Gianluca,
>
> I might misuderstand what you propose here, but for me the whole idea
> behind cond ? trueBranch : falseBranch is that it is a one-liner.
> Otherwise it is better suited for if-else-...
>
> Having said that, I can see that this way of formatting might make sense,
> if trueBranch and falseBranch are long(ish) String/GString|s (which we in
> our code do not have) - but as a general formatting rule, I would be
> against this.
>
> Cheers,
> mg
>
>
> Am 27.07.2026 um 14:38 schrieb Gianluca Sartori:
>
> Hi people,
>
> I've proposed the following PR to the Grails project in a rush, but I
> guess its proper place is the Groovy project.
>
> https://github.com/apache/grails-core/pull/16055
>
> We usually format ternary operators like this for readability:
>
> String message = condition
>         ? "Value when true"
>         : "Value when false"
>
> Would you accept such PR on the groovy-developer skill?
>
> Cheers,
> Gianluca
>
> Gianluca Sartori
> --
> https://dueuno.com
>
>
>

--0000000000004c55ac06579b5049
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto"><div>That&#39;s exactly how we use them: One-liners for s=
hort expressions and stacked for long ones, especially GStrings. Highly con=
text-specific.</div><div><br></div><div data-smartmail=3D"gmail_signature">=
<div dir=3D"ltr">Christopher Smith<br></div></div></div><br><div class=3D"g=
mail_quote gmail_quote_container"><div dir=3D"ltr" class=3D"gmail_attr">On =
Mon, Jul 27, 2026, 12:03 MG &lt;<a href=3D"mailto:mgbiz-yvYIh6MZAuFWk0Htik3J/[email protected]">mgbiz=
@arscreat.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" sty=
le=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><u></u=
>

 =20
   =20
 =20
  <div>
    Hi Gianluca,<br>
    <br>
    I might misuderstand what you propose here, but for me the whole
    idea behind cond ? trueBranch : falseBranch is that it is a
    one-liner.<br>
    Otherwise it is better suited for if-else-...<br>
    <br>
    Having said that, I can see that this way of formatting might make
    sense, if trueBranch and falseBranch are long(ish) String/GString|s
    (which we in our code do not have) - but as a general formatting
    rule, I would be against this.<br>
    <br>
    Cheers,<br>
    mg<br>
    <br>
    <br>
    <div>Am 27.07.2026 um 14:38 schrieb Gianluca
      Sartori:<br>
    </div>
    <blockquote type=3D"cite">
     =20
      <div dir=3D"ltr">
        <div>Hi people,<br clear=3D"all">
        </div>
        <div><br>
        </div>
        <div>I&#39;ve proposed the following PR to the Grails project in a
          rush, but I guess its proper=C2=A0place is the Groovy project.</d=
iv>
        <div><br>
        </div>
        <div><a href=3D"https://github.com/apache/grails-core/pull/16055" t=
arget=3D"_blank" rel=3D"noreferrer">https://github.com/apache/grails-core/p=
ull/16055</a></div>
        <div><br>
        </div>
        <div>We usually<span style=3D"background-color:transparent">=C2=A0f=
ormat
            ternary operators like this for readability:</span></div>
        <div><br>
        </div>
        <div>String message =3D condition<br>
          =C2=A0 =C2=A0 =C2=A0 =C2=A0 ? &quot;Value when true&quot;<br>
          =C2=A0 =C2=A0 =C2=A0 =C2=A0 : &quot;Value when false&quot;</div>
        <div><br>
        </div>
        <div>Would you accept such PR on the groovy-developer skill?</div>
        <div><br>
        </div>
        <div>Cheers,</div>
        <div>Gianluca</div>
        <div><br>
        </div>
        <div>
          <div dir=3D"ltr" class=3D"gmail_signature" data-smartmail=3D"gmai=
l_signature">
            <div dir=3D"ltr">Gianluca Sartori
              <div>--</div>
              <div><a href=3D"https://dueuno.com" target=3D"_blank" rel=3D"=
noreferrer">https://dueuno.com</a></div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
    <br>
  </div>

</blockquote></div>

--0000000000004c55ac06579b5049--