Monique said:
Objectively, I think you're wrong. I really mean that I think the
if/else syntax is clearer
You've got to be a bit careful here. There are (at least) two aspects to
clarity. One is how well does the programming construct capture the intent of
the programmer, the other is how clearly is that construct expressed in the
concrete syntax of the language.
In this case I think these two aspects work in different directions.
First off, if the concept is something like (for instance):
if the person is female then the retirement age is 60, otherwise
it's 65.
Note two things about that: (1) we are covering /all/ the possibilities (2) we
are using the two values (60 and 65) for the same purpose (as signified by the
fact that we don't -- and in normal English shouldn't -- repeat the phrase
"retirement age").
Now we can translate that concept into Java code using an if statement:
if (person.isFemale())
retirementAge = 60;
else
retirementAge = 65;
but that doesn't capture the concept particularly well. There is nothing to
stop us missing off the else (either syntactically or logically); and there is
nothing to hint that the important thing is that the same variable,
retirementAge, is assigned on both branches. So the notation is an /indirect/
expression of our intent. It works, but it's not as clear as it should be.
Using an ?: operator, OTOH, is a very direct expression of our intention:
retirementAge = person.isFemale()
? 60
: 65;
The reader doesn't have to /notice/ that the same variable is assigned to in
each case -- there is only one assignment. Similarly there is no possibility
of forgetting to cover the cases.
So, as far as that goes, the ?: operator (if used correctly) is significantly,
and objectively, clearer than an if statement.
But that's only half the story. /Given/ that a conditional expression is to be
preferred over conditional execution of different statements, does Java's
concrete syntax convey the underlying semantics well ? Now there, I agree, the
answer is an objective "no". It's something that you just have to get used to.
It belongs in the long tradition of punctuation abuse, which has been a central
feature of programming language design, more-or-less from the very beginning.
The syntactic unclarity is something that you /can/ get used to (especially if
you use decent layout conventions), and with experience you find the syntax
more and more transparent. But the unclarity in the other department is
inherent -- it's not something that goes away as you get used to it. If
anything it's the other way around -- as your sensitivity develops you find it
more and more off-putting.
-- chris