5

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Rob
  • 4,093
  • 5
  • 44
  • 54
  • Duplicate Question. Please always look into previously asked & answered questions frist. Here is the link, see if it helps: http://stackoverflow.com/questions/3622244/full-if-else-statement-vs-conditional-operator –  Nov 06 '10 at 14:20
  • 2
    Tip: Note that this particular ternary operator is usually called the conditional operator. A "ternary" operator is just an operator with 3 operands, hence the word "ternary". There aren't many ternary operators, but in languages that have multiple, the conditional one is just one of them. – Lasse V. Karlsen Nov 06 '10 at 14:20
  • @Mazhar: that question is about using `?:` in the normal, accepted way as an expression-level choice. This is about the abuse of `?:` to carry out active side-effects, to replace `if-else`. Personally I hate this practice so much. – bobince Nov 06 '10 at 14:43
  • >Duplicate Question. I don't believe so. Mine is specifically having to do with using it as a statement as opposed to an expression. The question linked is more general IMO. bobince is saying the same of course. – Rob Nov 06 '10 at 14:58
  • See http://stackoverflow.com/questions/522919/is-this-a-reasonable-use-of-the-ternary-operator – Crescent Fresh Nov 06 '10 at 15:29
  • The ( ternary | conditional | ?: ) operator is useful for simple string assignment. for example, when you want to pluralize a word dynamically. var str = num + (num > 1 ? ' Questions' : ' Question'); If you're trying to execute a statement, then use if-else. – zzzzBov Nov 06 '10 at 17:12

4 Answers4

3

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • so no technical reason why it can't be used then. – dewd Apr 22 '15 at 09:38
  • Worth distinguishing, also, between (1) a functional idiom of JavaScript, in which the key value of conditional expressions is that they **compose** in a way that conditional statements don't, and (2) an imperative style, in which expressions are not always required, and statements will often suffice. – houthakker Sep 23 '15 at 13:13
2

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)

Community
  • 1
  • 1
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • Wow, if they were to remove it per thinking it's "left over" code that would seem pretty aggressive IMO. The quote is interesting but assumes that the proposed use is in fact confusing the reader of the code. However, is the fact that it's less lines making it actually more readable? Not sure ;) – Rob Nov 06 '10 at 15:06
  • hm... less line don't necessarily make it more readable. For example, a program that is 35 lines may look more complicated than if it is 3 lines, but some of those code-golf 1-liner can be very hard to understand. – nonopolarity Nov 06 '10 at 16:37
0

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!

Jack
  • 131,802
  • 30
  • 241
  • 343
  • >silly - I think this is exactly why I asked it. I'm not used to seeing it in more classic languages but yes, it's allowed in JS. – Rob Nov 06 '10 at 14:55
0

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.

Thevs
  • 3,189
  • 2
  • 20
  • 32
  • I don't think mature has anything to do with it. I'd restate to argue that it is good for very simple conditions IMO – Rob Nov 06 '10 at 15:07
  • That very simple condition may be needed to extend at some unexpected time. – Thevs Nov 06 '10 at 15:16