Question

If you hate the ternary conditional operator in the first place, there's no need to reply ;)

I usually see this used in tandem with an assignment expression like:

var foo = (some_condition) ? then_code : else_code;

However, I'd like to use it to replace simple code like:

if(some_condition) {
  do_something_simple;
} else {
  do_something_else;
}

and instead do:

(some_condition) ? do_something_simple : do_something_else;

I'm likely to be doing this in JavaScript. In the above it returns undefined so it doesn't require the assignment. I like the space saved but wonder what folks think on this type of use as, again, I usually only see ternary used with an assignment.

EDIT: I've seen answers alluding to "hiding intent". Although classically used in expressions, how is this hiding the intent any more then using in an expression? Especially in a dynamic language where one may see the use of ternary operators all over the place?

Was it helpful?

Solution

The conditional operator should normally be used in expressions - value producing expressions - and is best not used as a replacement for the 'if/then/else' statement. Used occasionally, there'd be no particular problem; used systematically, I think it would be hiding the intent of the code from the readers.

OTHER TIPS

This is my personal preference:

In this case, I think it falls into the thinking "code is written for people to read, not for machine". Because most people don't write if then else this way, it may cause confusion, increased time to understand the code, and possibly introduce bugs -- if somebody saw that and thought, there is no assignment to any thing, must be "left over" code and let's remove it, then the code clean up becomes bug introduction.


Quoted from: Programs should be written for people to read, and only incidentally for machines to execute.

-- from "Structure and Interpretation of Computer Programs" by Abelson and Sussman

Charlie Martin said Is Code For Computers or for People?:

If the computer doesn't run it, it's broken. If people can't read it, it will be broken. Soon.

And I think yes, code is written for the machine to understand (and run properly), and it is also important for people to understand. (unless if it is written intentionally hard to understand to earn consulting fees, but they may hire someone else later or for next project, or written intentionally hard to understand for job security that if people can't understand your code well, they can't fire you fearing other people can't maintain the code... maybe there are 2 sides to a thing... i am seeing more and more cases like that)

The question is silly because you are talking about JavaScript which allows strange things.

The ternary conditional operator, in a classic programming language, requires that both cases are expression, not statements. In this way you can use it to choose between two expressions according to a boolean condition but not as a normal if/else branch..

in a language like JavaScript this difference vanishes because a statement actually returns a value so you can use the ternary and just discard the undefined value returned by your statement.

From my point of view, which resides more on other programming language, this can lead to confusion also if you save space but I think it's a matter of preference. Just don't get too used to it since just few programming language allows this kind of usage of the ternary operator!

Ternary operator is good for matured/stable programs, but not in constantly changing environment. Suppose you must add some extra code to any branch - it's much easier when you have if/then/else syntax.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top