c++ - Prettiness of ternary operator vs. if statement -


i'm browsing through code , found few ternary operators in it. code library use, , it's supposed quite fast.

i'm thinking if we're saving except space there.

what's experience?

performance

the ternary operator shouldn't differ in performance well-written equivalent if/else statement... may resolve same representation in abstract syntax tree, undergo same optimisations etc..

things can ? :

if you're initialising constant or reference, or working out value use inside member initialisation list, if/else statements can't used ? : can be:

const int x = f() ? 10 : 2;  x::x() : n_(n > 0 ? 2 * n : 0) { } 

factoring concise code

keys reasons use ? : include localisation, , avoiding redundantly repeating other parts of same statements/function-calls, example:

if (condition)     return x; else     return y; 

...is preferable to...

return condition ? x : y; 

...on readability grounds if dealing inexperienced programmers, or of terms complicated enough ? : structure gets lost in noise. in more complex cases like:

fn(condition1 ? t1 : f1, condition2 ? t2 : f2, condition3 ? t3 : f3); 

an equivalent if/else:

if (condition1)     if (condition2)         if (condition3)             fn(t1, t2, t3);         else             fn(t1, t2, f3);     else if (condition3)             fn(t1, f2, t3);         else             fn(t1, f2, f3); else     if (condition2)        ...etc... 

that's lot of function calls compiler may or may not optimise away.

can't named temporaries improve if/else monstrosity above?

if expressions t1, f1, t2 etc. verbose type repeatedly, creating named temporaries may help, then:

  • to performance matching ? : may need use std::move, except when same temporary passed 2 && parameters in function called: must avoid it. that's more complex , error-prone.

  • c ? x : y evaluates c either not both of x , y, makes safe test pointer isn't nullptr before using it, while providing fallback value/behaviour. code gets side effects of whichever of x , y selected. named temporaries, may need if / else around or ? : inside initialisation unwanted code executing, or code executing more desired.

functional difference: unifying result type

consider:

void is(int) { std::cout << "int\n"; } void is(double) { std::cout << "double\n"; }  void f(bool expr) {     is(expr ? 1 : 2.0);      if (expr)         is(1);     else         is(2.0); } 

in conditional operator version above, 1 undergoes standard conversion double type matched 2.0, meaning is(double) overload called true/1 situation. if/else statement doesn't trigger conversion: true/1 branch calls is(int).

you can't use expressions overall type of void in conditional operator either, whereas they're valid in statements under if/else.

emphasis: value-selection before/after action needing values

there's different emphasis:

an if/else statement emphasises branching first , what's done secondary, while ternary operator emphasises what's done on selection of values with.

in different situations, either may better reflect programmer's "natural" perspective on code , make easier understand, verify , maintain. may find selecting 1 on other based on order in consider these factors when writing code - if you've launched "doing something" find might use 1 of couple (or few) values with, ? : least disruptive way express , continue coding "flow".


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -