You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by Gianluca Sartori <g....@gmail.com> on 2026/07/27 12:38:32 UTC

Ternary operator formatting

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

Re: Ternary operator formatting

Posted by Christopher Smith <ch...@gmail.com>.
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 <mg...@arscreat.com> 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
>
>
>

Re: Ternary operator formatting

Posted by MG <mg...@arscreat.com>.
I think it could have been worded better to avoid any confusion, but no 
need to spam the ML with this, sent you a PM,
Cheers,
mg

Am 28.07.2026 um 22:49 schrieb Gianluca Sartori:
> Yes it is only for longer statements, I didn’t write it here, it was 
> 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 <mg...@arscreat.com> 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 readability" 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 <mg...@arscreat.com> 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
>>
>

Re: Ternary operator formatting

Posted by Gianluca Sartori <g....@gmail.com>.
Yes it is only for longer statements, I didn’t write it here, it was 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 <mg...@arscreat.com> 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 readability"
> 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 <mg...@arscreat.com> 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
>>
>>
>>
>

Re: Ternary operator formatting

Posted by MG <mg...@arscreat.com>.
Paul already cleared this up, so just a quick reply: I assumed from your 
post saying "We usually format ternary operators like this for 
readability" 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 <mg...@arscreat.com> 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
>

Re: Ternary operator formatting

Posted by Gianluca Sartori <g....@gmail.com>.
That makes sense. I assumed the Grails project was using the
`groovy-developer` skill from the Groovy project, my mistake.

Thank you for the clarification!

Gianluca Sartori
--
https://dueuno.com


On Tue, 28 Jul 2026 at 11:07, Paul King <pa...@asert.com.au> wrote:

> It probably is a Grails thing at this point. The Groovy project does
> have some general formatting guidelines but currently hasn't tried to
> provide a fixed recommended coding style across all language
> constructs. It is actually one of the things one agent has suggested
> we adopt to make life easier for AI agents, but we haven't look at
> that yet.
>
> On Tue, Jul 28, 2026 at 6:00 PM Gianluca Sartori <g....@gmail.com>
> wrote:
> >
> > 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 <mg...@arscreat.com> 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
> >>
> >>
>

Re: Ternary operator formatting

Posted by Paul King <pa...@asert.com.au>.
It probably is a Grails thing at this point. The Groovy project does
have some general formatting guidelines but currently hasn't tried to
provide a fixed recommended coding style across all language
constructs. It is actually one of the things one agent has suggested
we adopt to make life easier for AI agents, but we haven't look at
that yet.

On Tue, Jul 28, 2026 at 6:00 PM Gianluca Sartori <g....@gmail.com> wrote:
>
> 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 <mg...@arscreat.com> 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
>>
>>

Re: Ternary operator formatting

Posted by Gianluca Sartori <g....@gmail.com>.
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 <mg...@arscreat.com> 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
>
>
>

Re: Ternary operator formatting

Posted by MG <mg...@arscreat.com>.
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