|
Which just show that there are 10 types of people in the world ...
|
|
|
|
|
I know that 0 is false (or FALSE) and anything else is true (or TRUE)
disappointed to read through the comments
... old C++ days ...
since C++ defines "true" and "false" because defines the "bool" type
the trick comes around when the main function must return 0 if everything is OK
---------
Antonio
|
|
|
|
|
PinballWizard wrote: the main function must return 0 if everything is OK
That's why zero should mean true.
|
|
|
|
|
> if (isUDPSetup()==true)
You won't believe, but stupid hindu who wrote WPF (and .NET probably) follow logic like this! Look at ShowDialog() function - at WinForms time it was simple 'bool ShowDialog()' and code looked perfect: 'if (ShowDialog()) blah..'. Now they introduce... "bool?"!!! And code MUST look like "if (ShowDialog() == true) then...". You see that?! Really stupid....
|
|
|
|
|
Wouldn't this be more run-time efficient, as the value of update can be cached in a register ?
void setNeedsUpdate(bool update)
{
if (update == true)
{
NeedsUpdate= update;
}
else
{
NeedsUpdate= update;
}
}
;->
|
|
|
|
|
Or more straightforward:
void setNeedsUpdate(bool update)
{
NeedsUpdate = update;
}
Less confusing, and same behavior.
|
|
|
|
|
What about this:
void setNeedsUpdate(bool update)
{
if (update == (update == update))
{
NeedsUpdate= update;
}
else if (update == (update != update))
{
NeedsUpdate= update;
}
else
{
NeedsUpdate= update;
}
}
it has the advantage of using no constant at all and cleanly handles the case where update is not a boolean value.
I have added enlightening comments.
modified 2-Jan-12 5:06am.
|
|
|
|
|
The horrors I've seen:
void setVisible(bool isVisible)
{
if(isVisible.ToString().ToLower() == "true")
{
this.Visible = true;
}
if(isVisible.ToString().ToLower() == "false")
{
this.Visible = false;
}
}
|
|
|
|
|
You're making this up, aren't you?
|
|
|
|
|
I wish I was making this up.
Although it's good for a laugh (then a cry) when we go in to fix a bug in his old modules.
|
|
|
|
|
I hate this code too. Maybe in C you can do this but in C# it is a travesty. What I do is create self-documenting booleans so that you write code like this:
if (DoorIsOpen)
{
}
else
{
}
I feel that this eliminates the need for any of the crap above. It is true that the compiler doesn't care but just remember the the point of code is so humans can understand it. If we only cared about the computer understanding it, we would all be typing 1s and 0s. Write code so that those who come after you can read it without being distracted by how horrible it is.
"I don't believe it."
"That is why you fail." -- Empire Strikes Back
Shameless blog plug - www.geekswithblogs.net/jboyer
|
|
|
|
|
Let's see...
The first example I don't like - not for the if statement, but for the premature returns! Personally, I'd make it a one-liner, but I don't consider it all that horrible apart from the returns.
The second ... well you could argue it's slightly more readable for inexperienced programmers, but if that is your purpose then you should write an if/else statement, not use ?: .
The next is a double horror for comparing to boolean constants and unnecessary nesting, but I'd forgive the nesting for the off-chance of later maintenance introducing additional statements that depend on only the first condition.
The last ... I suppose if you need to be able to run your code through really old C compilers that don't have their own built-in definitions of bool , true and false , then it makes sense not to assign one boolean variable blindly to another, but instead make an if/else statement to catch the case where the argument is neither true nor false . But even then, ==true is horrible.
So, in short, all of the examples are indeed bad style, IMHO, but I agree with the others that no harm is done, except maybe a minor hit in maintenance effort for posting it in the Hall of Shame. None of this will cause inefficiency either, since compilers are smart enough to produce efficient code even from that kind of code.
|
|
|
|
|
They were probable taught to program by some CS grad student who'd never done much significant real-world coding. That's right along the lines of the kinds of stupidity my teachers would teach us when I was an undergrad. Like everybody else, I picked up the stupidity too.. which lasted until I saw the other way and had to debug code to a schedule that was broken by such nonsense.
I still do the compare to NULL sometimes, but I believe the C standard now defines NULL pointers as a false boolean value, so it is redundant and I'm trying to retrain away from it. Besides, boost smart pointers, which we use a lot in our code, have an override to generate a bool result for just such kinds of pointer checks and make comparing the raw pointer to NULL harder.
We can program with only 1's, but if all you've got are zeros, you've got nothing.
|
|
|
|
|
Personally, I don't promote the C rule that implicitly turns an expression to boolean based on zeroness, whatever the type. Because even though perfectly legal it looks like a quick & dirty shorthand to spare typing a comparison; and it can overload an identifier with two meanings, that of the numerical value (or address) and that of a condition, as if the variable had two data types.
What would you think of this (fiddled) snippet:
int NoRetries; NoRetries= SendMessage();
if (NoRetries)
{
}
as opposed to
if (NoRetries > 0)
{
}
C was lacking a boolean type in the old days, in my opinion a design flaw. That made the aforementioned rule perfectly relevant. I prefer making the booleans explicit and highlighted.
In a moderatly pedantic style, this would give us
bool Retried= NoRetries > 0;
if (Retried)
{
}
|
|
|
|
|
I absolutely agree with you and consider using the a-zero-int-value-is-a-bool-false feature of C/C++ to be sloppy. Valid, but sloppy. I consider it sloppy because 0 is a meaningful (and highly useful) value for an int.
However, with pointers, NULL is a meaningful value that indicates the pointer points to nothing, all other values are considered valid pointers. Using a pointer like a bool and checking if it is explicitly NULL are the same -- they are both asking if it points to anything. There is no other possible semantic interpretation.
Writing code that omits an explicit comparison to NULL is also more easier to upgrade if the pointer is switched to one of the boost smart pointer classes, something we've done a fair amount of over the years. Not that the compiler doesn't catch the now-invalid comparison, but then you have to touch all that code just to get the same semantic meaning.
We can program with only 1's, but if all you've got are zeros, you've got nothing.
|
|
|
|
|
Sorry, no exception even for convenience. An integer is an integer, a pointer is a pointer and a boolean is a boolean.
|
|
|
|
|
Maybe he saw this boolean[^], got confused, and wanted to "make sure".
|
|
|
|
|
Wow - that link is worthy of a top position in the Hall of Shame all by itself!
|
|
|
|
|
Hey, you're coding in a language that I don't use, but I'm totally with you. This is one of my pet peeves, too.
One of my languages is Visual FoxPro, which has the "Immediate IF" ternary function, IIF:
IIF(expr, a, b)
If expr is true, a is returned, otherwise b is returned. So what I see is people doing this:
MyBoolVar = IIF(SomeVar = 2, .t., .f.)
Which could be simplified to:
MyBoolVar = SomeVar = 2
Much cleaner. Or they expand it into a regular IF statement similar to what you posted. When I see this it does worry me. If programming should result from logical thinking and a programmer has trouble understanding logical values (boolean values), then should we wonder about the rest of their skills?
|
|
|
|
|
void setNeedsUpdate(bool update)
{
if ((update==true))
NeedsUpdate=true;
else
NeedsUpdate=false;
}
This would be better if they returned it too. Nothing like getting back what you put into it.
bool setNeedsUpdate(bool update)
{
if ((update==true))
NeedsUpdate=true;
else
NeedsUpdate=false;
return NeedsUpdate;
}
|
|
|
|
|
The second is better than the first. At least if you're going to set the value in this ridiculous fashion, make sure that it worked.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Excellent, in addition this provides a way to test that assignment succeeded and was correct.
I would even suggest
bool setNeedsUpdate(bool update)
{
if ((update==true))
{
NeedsUpdate=true;
return NeedsUpdate;
}
else
{
NeedsUpdate=false;
return NeedsUpdate;
}
}
so that if the code has a logic flaw, the function never returns !
|
|
|
|
|
How about "if (!(forceSend==true))" ?
|
|
|
|
|
I fully agree. This kind of stuff makes the code SO hard to read. And so many people do this too - just look at all the examples you were able to find! Atrocious.
When will people learn to put spaces around their operators !?!?!
Clive Pottinger
Victoria, BC
|
|
|
|
|
Just ran across this in code I've been asked to maintain (no kidding):
if (((ucGlobalHeaterEnable & (1 << UC_BHOSE_HTR_ON) ) > 0) ? 1 : 0)
{
...
}
Unbelievable!
|
|
|
|