- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
That looks distinctly like the result of a brain-dead coding standard.
Admin
"doesn't not unenlighten none of us"??
I couldn't fail to disagree with you less.
Admin
It's only a bit better than what IL produces when you recompile it. Have you ever seen what you get when you 'Reflector' compiled .NET code?
Here's an example:
if ((Array[i].SomeBoolean == false & SomeVar == SomeValue) != false)
{
//bla
}
Talking about obfuscating source code :-)
Admin
Not a double negative but ... I come across the following quite often, even in otherwise good demo or tutorial code. I'm not sure why ...
If bSomeBoolean = True Then
which of course is the same as
If bSomeBoolean Then
Admin
Just out of curiousity, I tried to see if actual double negatives will compile in VB.NET, using the website I'm working on.
If Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not Not User.Identity.IsAuthenticated Then
Gamedick.User.UpdateLastLoginDate(User.Identity.Name)
End If
It does compile, and the even number of nots boils down to being "If User.Identity..."
Admin
As a C++ developer, I still find this VB code amusing:
If Not SomeVar Is Nothing Then
VB should have a "IsNot" operator or a "Something" keyword. :-)
Admin
qwerty: if i remember correctly, the next version of vb has the isnot operator.
Admin
Ross,
<quote>
Not a double negative but ... I come across the following quite often, even in otherwise good demo or tutorial code. I'm not sure why ...
If bSomeBoolean = True Then
which of course is the same as
If bSomeBoolean Then
</quote>
They would (obviously) be the same if bSomeBoolean was of type boolean (which it presumably is - given the b at the front).
But don't forget they are not equivalent if bSomeBoolean is another type (such as Integer).
Seeya
Admin
Why isn't this guy using the DoNothing() procedure defined in an earlier wtf? =)
Admin
qwerty: VB 2005 does have an IsNot operator for exactly that kind of checking
Admin
At least it has not also got a commented like this:
' If it is not flaged as dirty do nothing otherwise update the account record.
If Not(blnIsDirty = True) Then
' Do Nothing
Else
UpdateAccountRecord
End If
Admin
In C++ I prefer to use:
if( b == TRUE ) ..
rather than just:
if( b ) ..
for dealing with old style bools, even though technically it'll work in the second case.
Admin
Regarding
if (expression)
and
if (expression=true)
there's actually often a difference between the two, especially when you take into account null references, non-boolean vars, etc. There's sometimes also a difference between being true, and not being false.
It may look messier, but if there's any chance of confusion I prefer explicitness.
Admin
Check out <a href="http://weblogs.asp.net/ericlippert/archive/2004/07/15/184431.aspx">http://weblogs.asp.net/ericlippert/archive/2004/07/15/184431.aspx</a>
Eric Lippart's thorough discussion of the issue.
Admin
Sorry, that was all a bit messy, wasn't it...
http://weblogs.asp.net/ericlippert/archive/2004/07/15/184431.aspx
Admin
In high performance software there are cases where you need that blank true clause. To help with the predictive branching, you want the true clause to be the most likely result. Thus you can get the construct of "if (x) {} else { work; }".
Then again, we are talking about VB here. :)
Admin
@Tim Smith,
really?
Shouldnt the compiler take care for such a case?
Devs shouldnt try to out smart the compiler...
// Ryan
Admin
I see this construct very often!
Its a product of evolution...
BEFORE:
' there was some code
' do some debug output... tracing...
AFTER:
' Do Nothing
someone has decided to delete some code and puts in there 'Do Nothing...
Admin
<b>'This comment is intentially left blank </b>
:-D
Admin
I kept seeing 'back to front' conditional code like
if (true == somevar)
{
' do stuff
}
in a c# app and didn't understand why until a c++ guy explained that in c++ if you put
if (true = somevar)
by mistake the compiler would throw an error, whereas the other way round it would compile.
Makes sense, but why carry it through to a strongly typed language? Old habits, I suppose...
Admin
Factory:
In C++ I prefer to use:
if( b == TRUE ) ..
rather than just:
if( b ) ..
FALSE (or false) in C++ is defined as a value of zero. TRUE is not false, which basically means anything other then zero. Explicitly comparing a variable to TRUE can be very dangerous...
You should either do this:
if ( b )
...
or if you must compare it to something, this:
if ( b != FALSE )
...
Admin
"TRUE" is not a C++ keyword, "true" is. Comparing something to "TRUE" is seeking trouble.
One has to also take into account that in many languages (expression == true) and (expression != 0) can be two different things and behave differently.
But anyways, nothing beats the overly cautious coder:
if(returnvalue == true)
{
return true;
}
else if(returnvalue == false)
{
return false;
}
Admin
Warp, "TRUE" is a keyword in VC++6.0 and later. I believe that it was changed because of the easier regognition from the code.
I also fully agree what Miszou said if it's not false it can be anything but 0 in c++.
Admin
Xarzith, that's actually incorrect TRUE is not a keyword in C++.
TRUE is a MACRO defined in afx.h
(#define TRUE 1)
However under VC 7.1, both the macro TRUE and the keyword true both evaluate to 1.
As can be seen with this snippet.
if(2 == true)
cout<<"true";
else
cout<<"false";
Output : false
If you change 2 to 1 it evaluates true as expected.
So I would have to disagree that if it is not false it can be anything.
I think the misunderstanding is how if works.
if(x == true) //will eval true only if x is true or 1
if(x) // will eval true as long as x is not zero.
Just my 2 cents.
Admin
I think tip #5 from John Robbins, (one of my favorite coder/authors) explains it well...
http://www.wintellect.com/resources/bugslayer/
he has another book on debugging .net ...
http://www.wintellect.com/resources/amazoninfo.aspx?asin=0735615365
David
(MCS)
Admin
In some file named afx.h as well as in many other places... you shouldn't generalize from the specific libraries and include file order you're using, unless they're standard in some way. afx.h is a (Windows only) header that plays some role in MFC, not a standard header.
Cheers, Sebastian
Admin
if (x = true) { ... }
will work just fine, including in C# (although Visual Studio will warn you), since if() expects a boolean, and it gets one. For extra amusement, consider the statement
if (x = false) { ... }
which will never actually execute. In any case, the relevant issue here is whether your language considers assignment statements to be expressions, or not. C# still does, so your example would still behave incorrectly.
Strong typing only matters in cases where the assignment type isn't a boolean, such as
if (a = 0xDEADBEEF) { ... }
where C would happily interpret the integer result as a boolean, while a strongly-typed language like C# would complain that you can't convert an UInt32 to a Boolean (interestingly, it doesn't even work with explicit cast, although some languages might).
That said, in any language, using
if (true == somevar) { ... }
is silly; just test the boolean directly instead of doing a comparison and testing the result. An early instructor of mine would have called it bad Boolean Zen.
Admin
Sounds like your instructor thought that "Zen" was a wild-card word -- you could stick it in anywhere and it would make sense.