Atom Feed SITE FEED   ADD TO GOOGLE READER

Java Minutiae - I love ternary

I love the ? : ternary operator. It makes my code feel tighter. Plus, I get to save code - fewer assignments, fewer return statements, and variable declarations are inline. In the code I was writing yesterday, a duplicated call to 'add()' made me itch for the ternary:
    for (String key : keys) {
if (getCardinality(key) == 1) {
singleValuedKeys.add(key);
} else {
multiValuedKeys.add(key);
}
}

Unfortunately, with standard use of a ternary, the code gets bigger instead of smaller:
    for (String key : keys) {
Set<String> setToAddTo = (getCardinality(key) == 1)
? singleValuedKeys
: multiValuedKeys;
setToAddTo.add(key);
}

Taking it one-step further, I inline the setToAddTo variable. This is where the code loses its familiar feel:
    for (String key : keys) {
((getCardinality(key) == 1)
? singleValuedKeys
: multiValuedKeys).add(key);
}

It's tight, but somehow I've written Java code that doesn't have the Java feel. So I ended up dropping the ternary, and went with the familiar if/else structure. What's your preference?
I love ternary, too, but I'd go w/ A or B.
I find readability suffers with all but the most trivial uses of ?:. I don't use it unless I can get it on one line, which is rarely given Java's love of trulyGignormousIdentifiers.

Scala's if *expression*, on the other hand, that I like.

x = if (test) { trueval }
else { falseval }

(In scala blocks are expressions returning the value of their last component expression.)
Your variable naming keeps the 3rd choice readable. I'd go for it. Sometimes it's good to write provocative code, just to see what happens. It can inspire weirdness in the people around you.
I love ternary and use it all the time, but might not in your case. Usually I find it not as clear if it's the message receiver that's being determined by the ternary (like what you had). But when it's a parameter:

foo.bar(blah ? 0 : 1)

or an assignment:

foo = bar ? 0 : 1

then I think it's quite readable.
I like B. For some reason the second statement is an "obvious place to stick a break point". And, I'd just stick things on one line. The setToAddTo name tells me "hey we're deciding upon a set"; I really don't need the rest of the linebreaks to help me scan.

But we are deciding upon a rule based on cardinality of the key. 'setMatchingKeyCardinality' is long, but more descriptive than 'setToAddTo'.
I come down on the side of C being difficult to read. Maybe that's because I don't love ternary! I don't write it myself, so when I read it, I find myself saying out loud "if...then...else". So, if you are working with mortal coders like me, consider keeping it simple.
For readability, if-else wins hands down.
Barath, I disagree. if/else often requires separation of variable declaration from assignment:

for (String key : keys) {
__Set<String> setToAddTo;
__if (getCardinality(key) == 1) {
____setToAddTo = singleValuedKeys;
__} else {
____setToAddTo = multiValuedKeys;
__}
__setToAddTo.add(key);
}

Only the ternary lets me combine declaration and assignment without duplicating the call to Set#add.