Re: Ternary operator formatting

Gianluca Sartori <[email protected]> Tue, 28 Jul 2026 22:49:07 +0200
Newsgroups gmane.comp.lang.groovy.devel
Message-ID <CAEJ7v=kFWesh-ke2Yz9D=8=n5yoQyMoExbb5drQfrWL_QWyLXw@mail.gmail.com>
--000000000000084bed0657b1f9ce
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Yes it is only for longer statements, I didn=E2=80=99t write it here, it wa=
s in the
linked PR:


   - Using ternary operators only for simple conditions.
   - Splitting longer expressions across multiple lines.
   - Aligning the ? and : branches for readability.



Gianluca Sartori
--
https://dueuno.com

On Tue, 28 Jul 2026 at 22:31, MG <mgbiz-yvYIh6MZAuFWk0Htik3J/[email protected]> wrote:

> Paul already cleared this up, so just a quick reply: I assumed from your
> post saying "We usually format ternary operators like this for readabilit=
y"
> that you meant in general - but if you meant only for longer (to be
> defined, so an LLM-bot gets it ;-) ) expressions, yes, why not G-)
>
> Cheers,
> mg
>
>
> Am 28.07.2026 um 10:00 schrieb Gianluca Sartori:
>
> That's the idea, to use the multiline style only when there are longer
> expressions like strings, method calls, etc.
>
> Anyway I just realized there is no `groovy-developer` skill in the groovy
> project, maybe Grails is the right place to work on it?
>
>
> Gianluca Sartori
> --
> https://dueuno.com
>
>
> On Mon, 27 Jul 2026 at 19: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 i=
n
>> 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 =3D condition
>>         ? "Value when true"
>>         : "Value when false"
>>
>> Would you accept such PR on the groovy-developer skill?
>>
>> Cheers,
>> Gianluca
>>
>> Gianluca Sartori
>> --
>> https://dueuno.com
>>
>>
>>
>

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

<div><div dir=3D"auto">Yes it is only for longer statements, I didn=E2=80=
=99t write it here, it was in the linked PR:</div></div><div style=3D"borde=
r-color:rgb(255,255,255)!important"><div dir=3D"auto"><br></div><div dir=3D=
"auto" style=3D"border-color:rgb(255,255,255)!important"><div style=3D"bord=
er-color:rgb(255,255,255)!important"><ul dir=3D"auto"><li>Using ternary ope=
rators only for simple conditions.<br></li><li>Splitting longer expressions=
 across multiple lines.<br></li><li><span style=3D"border-color:rgb(35,40,4=
4)">Aligning the ?</span><span style=3D"border-color:rgb(35,40,44)">=C2=A0a=
nd :</span><span style=3D"border-color:rgb(35,40,44)">=C2=A0branches for re=
adability.</span><br></li></ul></div></div><div><br clear=3D"all"><br clear=
=3D"all"><div><div dir=3D"ltr" class=3D"gmail_signature" data-smartmail=3D"=
gmail_signature"><div dir=3D"ltr">Gianluca Sartori<div>--</div><div><a href=
=3D"https://dueuno.com" target=3D"_blank">https://dueuno.com</a></div></div=
></div></div></div><div><br><div class=3D"gmail_quote"><div dir=3D"ltr" cla=
ss=3D"gmail_attr">On Tue, 28 Jul 2026 at 22:31, MG &lt;<a href=3D"mailto:mg=
biz-yvYIh6MZAuFWk0Htik3J/[email protected]" target=3D"_blank">mgbiz-yvYIh6MZAuFWk0Htik3J/[email protected]</a>&gt; wrote:<br></=
div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bor=
der-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-col=
or:rgb(204,204,204)"><u></u>

 =20
   =20
 =20
  <div>
    Paul already cleared this up, so just a quick reply: I assumed from
    your post saying &quot;We usually format ternary operators like this fo=
r
    readability&quot; that you meant in general - but if you meant only for
    longer (to be defined, so an LLM-bot gets it ;-) ) expressions, yes,
    why not G-)<br>
    <br>
    Cheers,<br>
    mg</div><div><br>
    <br>
    <div>Am 28.07.2026 um 10:00 schrieb Gianluca
      Sartori:<br>
    </div>
    <blockquote type=3D"cite">
     =20
      <div dir=3D"ltr">
        <div>That&#39;s the idea, to use the multiline style only when ther=
e
          are longer expressions like strings, method calls, etc.</div>
        <div><br>
        </div>
        <div>Anyway I just realized there is no `groovy-developer` skill
          in the groovy project, maybe Grails is the right place to work
          on it?</div>
        <div><br>
        </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">https:/=
/dueuno.com</a></div>
            </div>
          </div>
        </div>
        <br>
      </div>
      <br>
      <div class=3D"gmail_quote">
        <div dir=3D"ltr" class=3D"gmail_attr">On Mon, 27 Jul 2026 at 19:03,
          MG &lt;<a href=3D"mailto:mgbiz-yvYIh6MZAuFWk0Htik3J/[email protected]" target=3D"_blank">mg=
biz-yvYIh6MZAuFWk0Htik3J/[email protected]</a>&gt;
          wrote:<br>
        </div>
        <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex=
;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left=
-color:rgb(204,204,204)">
          <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">
              <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 t=
he
                  Groovy project.</div>
                <div><br>
                </div>
                <div><a href=3D"https://github.com/apache/grails-core/pull/=
16055" target=3D"_blank">https://github.com/apache/grails-core/pull/16055</=
a></div>
                <div><br>
                </div>
                <div>We usually<span style=3D"background-color:transparent"=
>=C2=A0format 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">
                    <div dir=3D"ltr">Gianluca Sartori
                      <div>--</div>
                      <div><a href=3D"https://dueuno.com" target=3D"_blank"=
>https://dueuno.com</a></div>
                    </div>
                  </div>
                </div>
              </div>
            </blockquote>
            <br>
          </div>
        </blockquote>
      </div>
    </blockquote>
    <br>
  </div>

</blockquote></div></div>
</div>

--000000000000084bed0657b1f9ce--