?: operator
-
coding style issues, which better, how used ?? 1. if (true == v1) { v2 = 0; } else { v2 = 1; } 2. v2 ((true == v1) ? 0 : 1);
Don't compare a boolean value with
true
orfalse
. There's no logic in that (sic!). It'll cause more confusion than clarity. For instance:if(((enabled == true) == true) == true...)
Where dou you stop? It's obviously clearer with
if(enabled)
But, as the question is formulated, me thinks that
v2 = v1 ? 0 : 1;
is best. -- The Blog: Bits and Pieces
-
coding style issues, which better, how used ?? 1. if (true == v1) { v2 = 0; } else { v2 = 1; } 2. v2 ((true == v1) ? 0 : 1);
-
coding style issues, which better, how used ?? 1. if (true == v1) { v2 = 0; } else { v2 = 1; } 2. v2 ((true == v1) ? 0 : 1);
if (v1) {
v2 = 0;
}
else {
v2 = 1;
}or
v2 = v1 ? 0 : 1;
For myself, I only use the condtional operator (
? :
) when the result is a simple expression.
Software Zen:
delete this;
-
Don't compare a boolean value with
true
orfalse
. There's no logic in that (sic!). It'll cause more confusion than clarity. For instance:if(((enabled == true) == true) == true...)
Where dou you stop? It's obviously clearer with
if(enabled)
But, as the question is formulated, me thinks that
v2 = v1 ? 0 : 1;
is best. -- The Blog: Bits and Pieces
I just want to add, it can also be a very buggy thing to do. Even a C++ 'bool' can have more values than just true and false. But aside from that, if you are working with Windows API, comparing to TRUE is the wrong thing to do since the API are defined as being 0 and != 0 which means that they might return 5219 for TRUE. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
Bob Stanneveld wrote: It's just a matter of preference. You can also write: v2 = !v1; Yes, when
v2
is also abool
, but I sensed that the question covered the generic case. -- The Blog: Bits and Pieces -
Bob Stanneveld wrote: It's just a matter of preference. You can also write: v2 = !v1; Yes, when
v2
is also abool
, but I sensed that the question covered the generic case. -- The Blog: Bits and Pieces -
coding style issues, which better, how used ?? 1. if (true == v1) { v2 = 0; } else { v2 = 1; } 2. v2 ((true == v1) ? 0 : 1);
if(v1) { v2 = 0; }
else { v2 = 1; }runs a little faster than
v2 = v1 ? 0 : 1;
Maxwell Chen
-
if(v1) { v2 = 0; }
else { v2 = 1; }runs a little faster than
v2 = v1 ? 0 : 1;
Maxwell Chen
Maxwell Chen wrote: if(v1) { v2 = 0; } else { v2 = 1; } runs a little faster than v2 = v1 ? 0 : 1; How thats run Faster?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
-
Maxwell Chen wrote: if(v1) { v2 = 0; } else { v2 = 1; } runs a little faster than v2 = v1 ? 0 : 1; How thats run Faster?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
About four years ago I did some test about your question with VC++6. Just now I did again with VC++7.1. My method is as:
void Test_If(bool& v1, int& v2)
{
if(v1) {
v2 = 0;
}
else {
v2 = 1;
}
}void Test_Op(bool& v1, int& v2)
{
v2 = v1 ? 0 : 1;
}void CTestComboDlg::OnBnClickedButton1()
{
bool v1 = true;
int v2 = 3;
int i;
DWORD ds, de;ds = GetTickCount(); for(i = 0; i < 0x10000000; i++) { Test\_If(v1, v2); } de = GetTickCount(); TRACE("IF: %u \\n", de - ds); ds = GetTickCount(); for(i = 0; i < 0x10000000; i++) { Test\_Op(v1, v2); } de = GetTickCount(); TRACE("OP: %u \\n", de - ds);
}
The output is:
IF: 48690
OP: 52576
Maxwell Chen
-
Maxwell Chen wrote: if(v1) { v2 = 0; } else { v2 = 1; } runs a little faster than v2 = v1 ? 0 : 1; How thats run Faster?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
ThatsAlok wrote: How thats run Faster? maybe because it has to evaluate the
?:
operator before.1. using if :
- evaluate the condition.
- test the if statement
- assign v2
2. using ?: :
- enters the = operator
- enters the ?: operator
- evaluate the condition
- return the right parameter wether the condition is true or false
- assign v2
i feel it like this... but i personnally still use the second case :-D
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
About four years ago I did some test about your question with VC++6. Just now I did again with VC++7.1. My method is as:
void Test_If(bool& v1, int& v2)
{
if(v1) {
v2 = 0;
}
else {
v2 = 1;
}
}void Test_Op(bool& v1, int& v2)
{
v2 = v1 ? 0 : 1;
}void CTestComboDlg::OnBnClickedButton1()
{
bool v1 = true;
int v2 = 3;
int i;
DWORD ds, de;ds = GetTickCount(); for(i = 0; i < 0x10000000; i++) { Test\_If(v1, v2); } de = GetTickCount(); TRACE("IF: %u \\n", de - ds); ds = GetTickCount(); for(i = 0; i < 0x10000000; i++) { Test\_Op(v1, v2); } de = GetTickCount(); TRACE("OP: %u \\n", de - ds);
}
The output is:
IF: 48690
OP: 52576
Maxwell Chen
Maxwell Chen wrote: he output is: IF: 48690 OP: 52576 Nice, Thanks for same :)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
-
ThatsAlok wrote: How thats run Faster? maybe because it has to evaluate the
?:
operator before.1. using if :
- evaluate the condition.
- test the if statement
- assign v2
2. using ?: :
- enters the = operator
- enters the ?: operator
- evaluate the condition
- return the right parameter wether the condition is true or false
- assign v2
i feel it like this... but i personnally still use the second case :-D
TOXCCT >>> GEII power
[toxcct][VisualCalc]You should not have to feel a thing. Just DO IT by examining the disassembly. The assembly dump would explain why one is faster than the other.