Ternary (conditional) operator and style

If you hate the ternary conditional operator in the first place, there is no need to answer;)

Usually I see that this is used in tandem with an assignment expression, for example:

var foo = (some_condition) ? then_code : else_code;

However, I would like to use it to replace simple code:

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

and instead:

(some_condition) ? do_something_simple : do_something_else;

I will probably do this in JavaScript. In the above example, it returns undefined, so it does not require an assignment. I like the space saved, but be surprised that people think about this type of use, because, again, I usually only see the triple used for the purpose.

EDIT: , " ". , , ? , ?

+5
4

- , , if if/then/else. ; , , .

+3

:

, " , , ". if then else , , , , - - , , "" " , .


: , .

- " "

?:

, . , . .

, , , ( ), , . ( , , - , , , ... , ... )

+2

, JavaScript, .

, , . , , if/else.

, JavaScript, , , undefined, .

, , , , . , !

0
source

The ternary operator is good for mature / stable programs, but not in an ever-changing environment. Suppose you have to add additional code to any branch - it is much easier if you have the syntax / then / else.

0
source

All Articles