Ternary operator

GDScript doesn’t support a proper ternary operator right now, which makes simple conditional assignments harder to read and more bloated than they need to be. This proposal is about adding a ternary operator like in many other modern languages.

Problem
Currently, we have to write this:

variable = value1 if condition else value2

This is hard to read - the condition sits awkwardly in the middle of the two values, and it breaks the natural flow for most devs. Compare that to a classic ternary:

variable = condition ? value1 : value2

Much cleaner, easier to parse, and takes less mental overhead.

Even worse, some people suggest writing it like this:

var variable
if condition:
  variable = "foo"
else:
  variable = "bar"

Are you serious? That’s 4 lines for something that should be one. For anyone writing real-world code, that’s just more noise and slower to maintain.

Why it matters?

  • Less boilerplate
  • Easier to read
  • Familiar to devs coming from JavaScript, TypeScript, C-like languages

Even Java has it - and Java is known for verbosity

Proposal
Introduce the ternary operator with the standard condition ? value1 : value2 syntax.

Bonus
This would make GDScript feel a lot more ergonomic and modern. It’s a small change with a big impact.

I think the first one is completely fine, but still if you want it as a feature, you can make a proposal in GIPs

1 Like

I don’t see any such thing as a difficult to read. I’ve just got this pretty clear logic.

when writing it is much easier because I add the condition as a parameter and I don’t have to enter anything anywhere

var variable = value1

and when I need to add a condition I just put it after the value as a “parameter”:

var variable = value1 if condition else value2

and if I don’t need it, I just delete the “parameter”:

var bariable = value1

so I see it something like this and on the whole I find it reasonable:

<declaration with value> <optional condition>

let’s not forget that GDScript is an interpreter, and the guys from godoteam probably had a reason to do it this way. my understanding is that the declaration and its value is understood implicitly and what is known is an option, so when GDScript is interpreted it has an implicit value, which is already assigned once and does not have to be parsed if there is something before or after, it just goes sequentially, and if it has the optional set as a condition, it just looks at the condition, and in case if it does not agree with the condition, it does not change anything and continues with the implicit value

for me everything is ok :slight_smile:

:godot:

1 Like

As @EX74 said, it seems to be same explanation as Python’s conditional expression, which uses the same syntax.
The Python documentation says:

This syntax may seem strange and backwards; why does the condition go in the middle of the expression, and not in the front as in C’s c ? x : y? The decision was checked by applying the new syntax to the modules in the standard library and seeing how the resulting code read. In many cases where a conditional expression is used, one value seems to be the ‘common case’ and one value is an ‘exceptional case’, used only on rarer occasions when the condition isn’t met.

Coming from C#, I can understand why people find the gdscript syntax more difficult to read that C’s one, but I think it’s actually just a question of habit.

I also think that if the gdscript ternary operator makes a code instruction that hard to read, well… maybe just replace it by an if/else statement :smile:

4 Likes

As someone coming from the TypeScript/JavaScript world and having spent almost 10 years in web dev, saying “GDScript is an interpreted language” just isn’t a valid argument. JavaScript is also interpreted, yet it has a ternary operator.

The issue isn’t just that you have to read it like value1 if condition else value2 it’s also just a bunch of boilerplate. I stopped using Java a long time ago for the same reason - writing extra code for no reason. This could be way simpler.
PS. Even Java has a ternary operator now.

Thank you! :muscle:

1 Like

Without the colon.
And, as others say, there’s nothing wrong with this syntax. You’ll get used to it.

Once you get used to something good in other languages, it’s hard to understand why it’s not used more often elsewhere. GDScript’s obsession with Python with everything backfires in some cases sadly :frowning:

Python is mainly used by the scientific community - and outside of building complex AI models, they’re not exactly known for building large-scale software with tons of business logic. Coming from that kind of background, I can say this becomes a problem fast. You end up with a messy one-liner - or worse, 4 lines of code - for something that should clearly be one line.

Overall, really sorry guys, I am really opinionated on this, and I truly want the language to grow and have great features. This is really not coming from bad intentions. :heart:

Just answering to that one as my suggestion may have been not clear enough, and that’s on me. (About the rest, you’ve sent your proposal, so let’s wait and see what’s happening there.)

I think the var = a if condition else b is fine as long as it’s used for short expressions. And I think that some people (not pinpointing you specifically, but I know it’s a common complaint) would like to reduce the number of lines in their code even if it means having a very long line.
And I think that while the gdscript/python’s syntax is readable enough for short lines, it’s terrible to read when it comes to longer statements. Which is why I said that in such a case, using a few more lines of code may be a solution. But that’s my personal opinion; I obviously like reducing the number of lines when possible, but if 4 more lines make it more readable, well, I prefer that.

Also, let me say that although I don’t think this ternary operator question is that much of a deal, I also prefer the var = condition ? a : b syntax and I’d be happy to see it added to gdscript, so I completely understand your request :smile: just trying to bring some arguments to the table!

Actually I don’t care that much because I’m using C#, not gdscript anymore. :shushing_face:

2 Likes

Thanks, yeah I agree with you. Huge apologies for sounding too dramatic here :pray:

But let’s see where this proposal takes us.

I honestly think this would really benefit the language

1 Like