You should read the thread before replying. It has been clearly explained that ? is currently only used for features related to nullability. Also using it as a conditional expression would result in an inconsistency.
I write on kotlin < 1 month, but i think syntax with if is not good. In functional languages - ok (as concept of language), but kotlin is not functional and is not declarative language, and if must no return value. if - control construction and no more. Syntax as classic ternary operator is very good readable and has no side-effect. one operator - one function (as linux strategy). And classic ternary is shortest and simplest. And if-syntax expression is associated with XQuery syntax, and this is not good. And support for ternary operator in kotlin must be simple (no intersect with other fetarures). My brain explodes from if-syntax.
Please return back ternary operator!
PS: instead of if with return can be used when-operator.
First, I disagree as you can use Kotlin as a functional language very well.
Second, Kotlin doesn’t like breaking their stuff, so changing the construction is not going to happen…
Adding is a possibility (which I personally don’t like in Kotlin)
So I came here because I was searching for Ternary operators in Kotlin. (Comming from languages that does have it)…
Considering the arguments, I think that it’s fair it’s not in the language, but I would very much like to see this mentioned in the Docs under FAQ or Comparing to Java, (If it already is, I could not immediately find it, It is mentioned under the control flow section though, here I find it odd)
I certainly have a personal preference leaning towards the ternary operators over the if/else expression examples… But that’s just it, personal preference, and nothing more - I honestly can’t see that turning into a objective argument of any sorts…
Arguments about the readability are silly - both those for and against… There are cases where you can be objective about readability in our field… This is not one of those cases IMO, there is so little difference, so which of the two constructs that is most readable will be highly dependable on who is reading…
Sorry, can’t let that one go since that is so wrong. Yes you can be totally objective on which is more readable. Saying it is dependent on who is reading has nothing to do with readability. Readability is by defintion objective. You can measure which people understand and how quickly. If one is more easily understood by more people then it is more readable.
You cannot assume that everyone understands the ternary operator. If I showed a line of code to my wife, who knows nothing about programming, which do you think she would understand the ternary operator or an if-else? I think there is no doubt it is the latter. You may counter that she isn’t a programmer but the fact remains if-else will be understandable to more people than the ternary operator.
I am not saying that there shouldn’t be constructs that are obscure until you learn them, but saying that there is no difference in readability here is false.
Sorry, but readabilty and the ternary operator has been a hot button issue for me for over 20 years. Many years ago I purposefully tried not to know the order of ? and : so I would never use the ternary operator because it was less readable and that was long before nice things like when and if expressions.
So your saying that those in this thread that says the ternary operator is more readable to them are wrong? That is just arrogant or ignorant.
You can certainly speak of what a majority of a group would find more readable, but what should that group of people consist of? again we are back to having opinions on how to select that group so we can measure it.
Here if find it totally silly to include your wife, would you think its fair to measure the readability of the french language within a group of Americans and on that conclude that it’s a horrible language because no one or just a few seemed to be able to understand? (although it may win over mandarin in that same group because some words is familiar)
The human brain doesn’t read words, it excels in recognizing patterns, so while a = if b else c; is likely easier to learn to begin with than a ? b : c; because there are patterns that is already known, if your english speaking at least. When learned, my argument is that there is so little difference in which will be more readable to you, that it doesn’t make sense to argue about.
Only in the use of terminology. Humans are very adept at pattern recognition and with repeated exposure humans can learn to recognize, understand, and prefer very cryptic patterns (after all there are actually people who can understand and prefer to code in Perl ;-). I do think it is wrong to associate the term readability to that since readability by a particular individual is kind of a contradiction of terms.
You can ONLY speak about the majority of a group when talking about readability. Readability is all about what is most understandable to the widest audience. It is incorrect to talk about readability in terms of an individual.
You certainly do have to know your target audience. A child’s book is definitely more readable by every objective measure, but you would not write a college textbook that way. But it would be incorrect to say that targeting a more specific is increasing readability.
Not silly at all just an extreme example to illustrate the point. Among people in general as opposed to those who have experience with C-like languages I think we can safely conclude that the ternary operator would be less readable. Among all programmers (not just those coming from C-like languages) I think the same would be true, just not as extreme. Among coders that compete in C obfuscation contests probably no difference.
Readability is not about labeling things with value statements like good, bad, horrible, but about what is the best way to communicate things to other humans. Like Martin Fowler says, “Any fool can write code that a computer can understand. Good programmers write code that humans can understand”
What one could conclude is that if you had something you wanted to communicate to the majority of people living in America that doing so in French would not be an effective way to do that. You would likely want to communicate it in English and probably Spanish to be understood by the most people. That doesn’t deny the fact that there may be people in the US that only speak French, but they are in the minority. If I were trying to communicate to people living in France then I would find I probably would want to use French and English.
Requiring someone to learn == less readable. Sometimes you accept less readable because it gives you more power and expressiveness. E.g. The safe call operator (?.) is less readable for someone coming from many other languages, but it is so powerful that we accept that one has to learn it.
My point was not to argue about ternary operator itself (though I am in the “good riddance” camp), but someone saying that readability is subjective and cannot be measured is a hot button issue for me.
Since Kotlin has if expressions the ternary operator’s only advantage (other than the insignificant number of characters saved) is familiarity to some programmers coming from languages that had it. It offers no new power (and with the use of ? for nullability would likely cause parsing problems). If expressions offer the advantage of being more easily understood by most any programmer,while granted they will be unfamiliar to those programmers who are adept at the ternary operator (which is likely a minority of the group of all programmers and as someone said, “when learned there is so little difference”).
That is funny, just taking a quote from my first post:
That is stating quite the opposite of what your saying I am saying is it not?..
My point is, that IF you measure the readability of the two different constructs presented here, for a group I find suitable, e.g. all new programmers right out of school… My guess is that it will be close to a tie… Unless you have evidence against that… My guess here is as good as yours…
Now IF my guess happens to be correct, then arguments about readability in this particular case become subjective as we lack the evidence either for or against.
No, it is exactly what I am saying you are saying and the whole reason I even replied. You said that the readability of the ternary operator is not a case where one can be objective, that its readability is dependent on who is reading, i.e. it is subjective.
No, that does not make it subjective, it makes it speculative. Without measuring it (or more precisely taking measurements to make statistically significant estimates) we have to speculate. But that is not guesswork, we can make some reasonable, educated guesses based on the evidence of experience.
Let’s break your group down into 4 subsets:
Those that are unexposed and clueless on ternary operator
Those that can understand the ternary operator but it takes them longer to decipher the ternary operator over an if-else
Those that decipher both equally
Those that are actually better at understanding ternary than an if-else
The question then becomes what are the relative sizes of these 4 groups.
For group 1, this group is closer to the example of my wife. Even among non-programmers they are more likely to understand an if-else.
Group 2 is where I fall and I suspect most programmers would fall. And I am not a programmer fresh out of school. I have been programming over 35 years and in Java for over 20 years, but it still takes me slightly longer to mentally parse the ternary operator.
You mentioned having to learn, but all this does is move people from group 1 to group 2, since with no training at all if-else is already more readable. It would take a lot of work to move people from group 1 to group 3 or 4
My estimates on the 4 groups are probably 15%, 70%, 10%, 5%. Those are my own speculations based on my experience, not guesswork. They are not subjective in that they are not based on my own preferences or a particular individual. I could be totally wrong, but I see no evidence to the contrary or to suggest that the numbers even make it anywhere near a tie.
Unless you have strong evidence that the vast majority programmers fall into groups 3 & 4, which would contradict all experience I have, then the idea that the readability is even close to a tie is unfounded.
Even if group 3 were the majority of programmers, Kotlin has if-expressions, which are equally readable to them. The notion that Kotlin should have the ternary operator can only really be made if the majority of programmers fall into group 4 and I am confident that is not the case.
The fact that ternary operator is used sparingly in Java where it actually adds something to the language (since Java does not have is expressions) is further evidence that most programmers fall on the group 1 and group 2 end of the spectrum.
Besides, it’s exactly what I am doing… Making an educated guess based on my experience… That experience unfortunately does not match yours…
I have to present evidence of that our arguments are not not based in facts, but rather speculations and guesswork? o.O…
My Experience contradict yours…
Every applicant we get leaves me with that experience, especially drawing on our intern applicants where I get the same picture of our educations as when I was in school my self, which is that close to all (I don’t like saying all even though I have not meet anyone who falls out of that) students within IT are being teached in C like languages as a primary (for the programming part). And with that, knowledge of the ternary operator. (I haven’t meet anyone who didn’t understand that syntax, I can’t say if it has been thought or was intuitive)
I don’t have any evidence beyond my experience, but it sounds like neither does you.
Pls don’t start this discussion again. I am pretty sure everything has been said about the “but it saves x characters” argument on both sides. So as long as there is not a new one there is no reason to get into it again