Ternary operator

Which is immaterial. The question is Kotlin, where ? is 100% associated with nullability.

And I see no profit in having that restriction.
Every programmer reuse his knowledge of natural languages, math notation, other programming languages.
Say ? will always be associated only with nulls in kotlin. But not in programmers mind.
What would be the point in supporting that made up rule?
Simpler learning? I doubt it.
What new information will you give to other person, who don’t know kotlin, when you tell him that rule? None. Just some curious fact, an anecdote.

I did not say it as a restriction but a statement of fact. ? Already is used in the Kotlin language and 100% of those uses involve nullability. In Java ? is only used as the conditional operator. Adding a non-nullable use for it leads to confusion.

Couple of other points:

Your natural language example was used to express whether Mark was going to be there. That is actually what Kotlin uses it for, to express that something may not be there (may be null).

And in natural language when we want to express 2 alternatives based on whether something is true or not we do something like, “if it is raining then drive to work else bike to work”. Note that no question marks are used to express that. It is not natural to instead write, “it is raining? drive to work: bike to work”. Kotlin lets you express it in a way that is closer to natural language, “if(itIsRaining) driveToWork() else bike to work()”.

Your argument that ? : is natural language is not supportable.

I think you considered english only :slight_smile:

Well programming lan[quote=“guai, post:156, topic:2116”]
I think you considered english only :slight_smile:
[/quote]

Programming languages tend to be based on english and this has a few advantages. Every international software company has to decide on one language to use and this is most of the time english. Mixing languages in code get’s confusing. I once had to deal with a smalltalk program where everything was named in german. I speak german so this is not a problem for me but it still looks strange.

There are many more non-international software companies than international ones. Mixing languages can carry additional information, like we use national language for busyness logic and english for core code. When there are several teams of programmers its better to use national terminology than several variants of translation or (god forbid) transliteration.
Besides, programmers still think in their native languages, math notation, other programming languages that they learned before kotlin, many of which have ternary.
BTW you can mix programming languages in one project too. In case of kotlin it most certainly be java or groovy, both of which have ternary.
For that reason I don’t really like kotlin’s types on the wrong side, but thats another story

I don’t mind using multiple programming languages in a project. The problem I have if I have to switch languages mid expression multiple times.

Also maybe I did not explain my point properly. Programming languages are based on english as most people using them can speak it. Adding features to Kotlin based on languages most people don’t speak will just confuse them.

I’m really glad that Kotlin doesn’t have the ternary operator since it has been a source of frustration in Java due to:

  • Nested ternaries are difficult to read and easy to misinterpret
  • Single ternary that doesn’t fit on one line is difficult to read
  • Accidentally inverting the ternary by putting the false case first (I have seen this)
  • There’s always the odd developer that likes to abuse the ternary operator and makes it difficult for the rest of the team

I use this as a selling point when promoting Kotlin (in addition to the larger benefits)

1 Like

Me too! I’m glad others are selling Kotlin on this. It surprises me that people still prefer the ternary operator over if-expressions after they’ve tried them. Would those people also prefer Python’s ternary operator, “a if condition else b”?

I feel this topic has been well summarized several times by multiple people over the last year. Should this topic be closed?

2 Likes

And you ignored everything I said that refutes your claim to make a nonsensical reply.

Kotlin keywords are based on English so yes I of course stuck with English just as you did. Are you claiming there is some non-English natural language where one routinely says “it is raining? drive to work: bike to work”

1 Like

I’m claiming that ternary is quite familiar concept for most programmers either from other langs from largely grown C-family or natural languages and was never broken. Unless of course deep nesting of ternary. Which is also true for if and any other statement since 5-6 levels of nested ifs is not so readable too.
But thats ok cause IDE can fix it in a moment and replace ternary with ifs.
And lack of ternary is especially annoying on multi-language projects.
I’m also claiming that sticking to the rule “? always relate to null” adds nothing to kotlin.

1 Like

as actual proof of your words “I’m claiming that ternary is quite familiar concept for most programmers” is swift lang: swift uses ? for optional chaining(Optional Chaining — The Swift Programming Language (Swift 5.7)) like this:
if let roomCount = john.residence?.numberOfRooms {}
and swift uses ? for ternary operator.
But, need kotlin designers to add ternary, they would already add it in kotlin.

Actually that was not the argument you were making. That is the ONLY valid argument for ternary operator, but is not a particularly compelling one. There is plenty familiar to Java programmers that were jettisoned in Koitlin for good reason.

For those not coming from a C-based language it is very unfamiliar. if-else is familiar to both.

And we have to ask why did it exist in those C-derivative languages? Because it filled an important role because if was a statement in those languages and not an expression. Since Kotlin eliminated that problem and if is an expression there is no need to use the confusing hack of the ternary operator.

That was the actual claim you did make but did not adequately support and is not valid. Unless you can produce a natural language that uses ? : as a ternary expression then your claim is invalid.

Actually it was always broken and a bad hack. See: ternary operator considered harmful - Google Search

Adding the ternary operator would add nothing to Kotlin as it would be a redundant construct that is non-obvious to those unfamiliar with it. It would detract from Kotlin in that it adds confusion by giving an alternate meaning to ?.

And once again they did that because, like Java, C and other languages, they made “if” a STATEMENT not an EXPRESSION. When if is an expression as it is in Kotlin then the ternary operator would just be a an ugly, non-obvious alternative to the much more clear if-expression.

I’ve created a topic to discuss closing this topic and the companion object topic: Please close the mega topics (ternary and companion objects)

2 Likes

Then why language designers adding it over and over again? Swift, groovy, js, C#. I bet there are many more

I never claimed that there are natural languages with that exact syntax, it was you imagination. I claimed that in natural languages there are similar constructs, and anyone can recognize this constructs in ternary.

Why ruin all the fun?

And who would be that people writing on jvm-based lang, stated to be better java, stated to be targeting java developers, and unfamiliar with ternary?
Anyway its no big deal to grasp it

Reasons to not add the ternary operator:

  • It doesn’t add any new functionality
  • If-expressions work okay as they are now
  • It can lead to unclear code when chaining ternaries
  • It can lead to unclear code when using long expressions
  • It is difficult to understand for those new to programming
  • It clashes with Nullability’s use of the question mark
  • It clashes with potential uses of the colon
  • The minus 100 point rule

Reasons to add the ternary operator:

  • It’s more concise (shorter character-wise)
  • It’s expected by users coming from other languages
  • It’s understood by users coming from other languages
  • It does not clash with Nullability’s question mark (see other languages)
  • It doesn’t limit other future uses of the colon

Reasons to end the fun:

5 Likes

Because they failed to fix the fundamental problem that if was a statement rather than an expression. So they just fell into copying the ugly alternative rather than fix the problem.

This is what you said:

And yet you provided no examples of this. You provided an example that agrees with Kotlin’s use of ? for nullability. I provided an example where natural language uses an if-else style of syntax.

But I agree with arocnies, stop this thread.