Not really a horror, but useless all the same
-
Came across this just the other day:
bool value = (someObject == NULL)? true : false;
The same thing is dotted around in a few places, some of the the other way around (returning false if the statement is true). I just can't understand why anybody would bother.My current favourite word is: Delicious!
-SK Genius
-
Came across this just the other day:
bool value = (someObject == NULL)? true : false;
The same thing is dotted around in a few places, some of the the other way around (returning false if the statement is true). I just can't understand why anybody would bother.My current favourite word is: Delicious!
-SK Genius
SK Genius wrote:
I just can't understand why anybody would bother.
What on earth is wrong with a ternary operation[^]? There are valid reasons for using it. It is a hint to the compiler that the software engineer wants to make an assignment with no conditional branching if possible. The compiler will try to perform an optimization such as ternary operator optimization[^] if it is possible. I guess he could have written it like this and it would possibly optimized the same:
bool value;
if(NULL == someObject)
value = true;
else
value = false;But who really cares? They are equally readable to me although I prefer the ternary operation. :) Best Wishes, -David Delaune
-
SK Genius wrote:
I just can't understand why anybody would bother.
What on earth is wrong with a ternary operation[^]? There are valid reasons for using it. It is a hint to the compiler that the software engineer wants to make an assignment with no conditional branching if possible. The compiler will try to perform an optimization such as ternary operator optimization[^] if it is possible. I guess he could have written it like this and it would possibly optimized the same:
bool value;
if(NULL == someObject)
value = true;
else
value = false;But who really cares? They are equally readable to me although I prefer the ternary operation. :) Best Wishes, -David Delaune
-
Nothing wrong with ternary operation really. But wouldn't
(NULL == someObject)
returns True/False that can be assigned straight to "value"?
-
SK Genius wrote:
I just can't understand why anybody would bother.
What on earth is wrong with a ternary operation[^]? There are valid reasons for using it. It is a hint to the compiler that the software engineer wants to make an assignment with no conditional branching if possible. The compiler will try to perform an optimization such as ternary operator optimization[^] if it is possible. I guess he could have written it like this and it would possibly optimized the same:
bool value;
if(NULL == someObject)
value = true;
else
value = false;But who really cares? They are equally readable to me although I prefer the ternary operation. :) Best Wishes, -David Delaune
But it's not necessary in this case.
-
DePatrick wrote:
returns True/False that can be assigned straight to "value"?
Yep, that is true. Best Wishes, -David Delaune
Randor wrote:
that is true
I second that; it is absolutely
(true==!false)==true?true:!false
:)Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Came across this just the other day:
bool value = (someObject == NULL)? true : false;
The same thing is dotted around in a few places, some of the the other way around (returning false if the statement is true). I just can't understand why anybody would bother.My current favourite word is: Delicious!
-SK Genius
It is probably a misunderstanding of the form: BOOL value = (someObject == NULL) ? TRUE : FALSE; where #define FALSE 0 #define TRUE !(FALSE)
m.bergman
-- For Bruce Schneier, quanta only have one state : afraid.
-
Came across this just the other day:
bool value = (someObject == NULL)? true : false;
The same thing is dotted around in a few places, some of the the other way around (returning false if the statement is true). I just can't understand why anybody would bother.My current favourite word is: Delicious!
-SK Genius
I don't see the horror...