3 ms·
"I find if(...){} much more readable." Not only is it more readable, it's more correct in the example given. "if" is a statement. The ternary operator (?:) is
by xer0 15y ago
"I find if(...){} much more readable."
Not only is it more readable, it's more correct in the example given.
"if" is a statement. The ternary operator (?:) is an expression, that returns a value. The semantics are different, and using the ternary operator "merely" to replace an if statement, especially where no assignment of the ternary operator's return value is made, is a code smell.
The ternary operator is great if you're replacing a simple "if something assignment to X else other assignment to X." Any other use is questionable.