|
I've started to favour this form:
#define TRUE (!0)
#define FALSE (!TRUE)
But at least I put it in a header rather than within every single source file. In this case, it was in *.c. Can you say "maintenance nightmare"?
And they claimed they were a CMM level 5 company, too.
Professional Geek,
Amateur Stage-Levelling Gauge
modified on Wednesday, February 4, 2009 6:19 PM
|
|
|
|
|
The (0==0) may be more portable, which is what they were after.
I just tried (0==0) and (!0) on two compilers and both agreed that they equal 1, but that may not always be (or have been) the case.
|
|
|
|
|
Many C compilers (and ancient C++ compilers) don't support the bool type. The expression (0==0) has the advantage of having type bool when possible and int otherwise.
|
|
|
|
|
I would love to have macro-definitions in C# or Java just to break all these silly stereotypes.
#define max(a,b) (a>b ? b : a)
#define min(a,b) (a>b ? a : b) or
#define cerr (cin)
#define cout (cerr)
#define cin (cout)
Aaah so confusing so sweet ...
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
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
Game Programming articles start - here[ ^]-
|
|
|
|
|
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"?
|
|
|
|
|
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
|
|
|
|
|
But it's not necessary in this case.
|
|
|
|
|
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.
|
|
|
|
|
I don't see the horror...
|
|
|
|
|
Found in my own code:
if (action == InvalidContentActions.Remove || action == InvalidContentActions.Remove) {
...
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
More like a second chance.
Yesterday I somehow wound up writing if ( x != x )... , I enjoyed the warning I got:
warning CS1718: Comparison made to same variable; did you mean to compare something else?
|
|
|
|
|
You could actually write something like that to show the dangers of unsafe multithreading :p
|
|
|
|
|
I've seriously used code like that to check for NaN.
|
|
|
|
|
I've seriously used code like that to check for NaN.
If A and B are both floating-point NaN's related to the same condition, what are the values of the six relational operators? If they all report false, then one could test if two numbers were both equal or both NaN with "if (!(a != b))"; if NaN's report not equal to each other but neither greater- nor less-than each other, then maybe "if (!(a > b) && !(a < b))". Of course, unless such statements are commented people would likely find them confusing.
|
|
|
|
|
Wouldn't float.IsNaN(x) be a more readable way to achieve that?
|
|
|
|
|
Only decimal numbers have NaN as static class member (at least ni C#), natural numbers like int or short don't have it.
Though if something is NaN, you would get an error the moment it's assigned to a natural number variable.
|
|
|
|
|
That is a good test for a custom "!=" operator
Greetings from Germany
|
|
|
|
|
If action is volatile, it would be possible for the second test to succeed even if the first fails. Indeed, if it's a memory-mapped mapped register, such behavior might even be expected.
|
|
|
|
|
That's true.
But in that case it has to be made explicit in the code, either by using comments or a critical section or whatever may be appropriate.
Otherwise it would indeed be a very severe coding horror (since it could cause an error that would be very hard to find...) .
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Reminds me of that little DOS / Win console classic to wait for a keystroke:
if (!getch()) getch();
yes, this is the right way to do it
|
|
|
|
|
There should be more code like your example. I'm all for double checking things instead of just single checking.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
|
|
|
|
|
I agree. In this day and age you can never be sure of anything...
DoesJessicaAlbaLoveMe uncertain = new DoesJessicaAlbaLoveMe();
string yahhhoooooo = "";
if (DoesJessicaAlbaLoveMe != null && DoesJessicaAlbaLoveMe!= null)
{
yahhhoooooo = uncertain.ToString().ToString().ToString();
}
else
{
yahoo = "She's just teasin :P";
}
A dogged, arrogant belief in self and the childlike idealism that comes with not knowing my limits. This is my greatest blessing, my priceless attribute.
|
|
|
|