That the code snippet
if (addMore) {
y = x + moreValue;
} else {
y = x;
}
can be compressed into:
y = (addMore ? x + moreValue : x);
Is familiar to most programmers with a some experience with Java.
But I was wondering if more than two states can be compressed into one line, eg:
if (addMore) {
y = x + moreValue;
} else if (50 < x) {
y = 50;
} else {
y = x;
}
How can this (if possible that is) be compressed into one statement, something like:
y = (addMore ? x + moreValue {SOMETHING IN HERE} : x);
I use such a construct regurarly, it is reminiscent of Clojure’s
cond:I find no issues with readabiility, quite to the contrary. I like that I can be sure this whole construct is about assigning a value to y. That is not at all obvious with the classic if-else if combination.