Quantcast
Channel: What are pros/cons of ternary conditional operators? - Programming Language Design and Implementation Stack Exchange
Viewing all articles
Browse latest Browse all 10

Answer by Audrius Meškauskas for What are pros/cons of ternary conditional operators?

$
0
0

If statement duplicates the part specifying what to do with the conditionally selected value:

if (condition) {   use_value_now(alpha, beta, 0);} else {    use_value_now(alpha, beta, 1);}

It may be OK when the selected value is just assigned to the variable, but for the function call as in this example it almost looks like a dedicated variable should be defined for it. Then it is not so clear how (and if) this variable should be initialized when declared. Curly brackets (or they analogs in other languages) are also now mostly a requirement for the good coding style.

Compare this with

use_value_now(alpha, beta, condition ? 0 : 1);

Viewing all articles
Browse latest Browse all 10

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>