问题
I have a doubt in the following scenario (C++):
Say, I have an if condition
if ( a ? b ? c : d : false)
{
// do something
}
else
{
// do something else
}
This is my interpretation of how it works:
If a is true, it checks b. Then,
- If b is true, the if loop is reduced to if (c)
- If b is false, the if loop is reduced to if (d)
If a is false, the if loop is reduced to if (false)
Is my understanding correct?
Is using this better or multiple if
/else
checks?
回答1:
Please use this in parenthesis, as it helps improve readability. Also, it is fine using multiple ternary operators.
if ( a ? (b ? c : d) : false)
{
// do something
}
else
{
// do something else
}
回答2:
Your question is in two parts, firstly about the behavior of the statement, and then about whether you should do this; allow me to show you how many programmers would solve the second.
Imagine it's 4.30am on a Saturday morning and you're hung over and there is a bug in this code, and you need it fixed within the next 30 minutes or your job/business is at risk.
if (a ? b ? c : d : false)
or
if (a) {
if (b)
return c;
else
return d;
} else {
return false;
}
or
if (!a)
return false;
if (!b)
return d;
return c;
or
if (a)
return b ? c : d;
else
return false;
Which was the right choice?
回答3:
Yes, you are correct at stating:
If a is true, it checks b. Then,
- If b is true, the if condition is reduced to if (c)
- If b is false, the if condition is reduced to if (d)
If a is false, the if condition is reduced to if (false)
but if() is not a loop but a condition statement. and writing condition in parenthesis make it more readable.
if ( a ? (b ? c : d) : false)
or you can simplify it:
if ( a && (b ? c : d))
回答4:
Is my understanding correct?
Yes, your interpretation is correct.
Is using this better or multiple if/else checks?
As you know the code:
if ( a ? b ? c : d : false)
{
"Code-1"
}
else
{
"Code-2"
}
could be written like:
if(a){
if(b){
if (c){
"code-1"
}
else{
"code-2"
}
}
else{
if(d){
"code-1"
}
else{
"code-2"
}
}
}
else{
//then false;
"Code-2"
}
Now which you will prefer in above two. Second is long and includes many nested level code (hard to understand and bug). Additionally first code that can be improved in readability as:
if ( a ? (b ? c : d) : false)
as @ManikandanSigamani answered.
As I noted @WhozCraig also give an another technique to write your code better if (a && (b ? c : d))
using &&
that supports Short-Circuit logic.
A good programmer is one who know one than one technique to solve a problem and also know which is better. A short and linear codding is preferable in general. In small writing small code chances of making mistakes are less (looks like functional programming better then imperative programming). And small code easily can improved as in your case suggested by @ManikandanSigamani and @WhozCraig.
I will prefer: if (a && (b ? c : d))
form!
回答5:
Though it works, nesting ternary operator like this is seldom readable.
Writing it as
if ( a && (( b && c ) || d )) {
// do something
} else {
// do something else
}
is, imho, much more readable and it is not much longer than your original code.
来源:https://stackoverflow.com/questions/19020092/multiple-ternary-operator-in-c