• Ben Hutchings (unregistered)

    That looks distinctly like the result of a brain-dead coding standard.

  • John Sands (unregistered)

    "doesn't not unenlighten none of us"??

    I couldn't fail to disagree with you less.

  • Reinhard Brongers (unregistered)

    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 :-)

  • Ross (unregistered)

    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

  • Jake Vinson (unregistered)

    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..."

  • qwerty (unregistered)

    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. :-)

  • ko (unregistered)

    qwerty: if i remember correctly, the next version of vb has the isnot operator.

  • mjwills (unregistered)

    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

  • Stian S&#248;iland (unregistered)

    Why isn't this guy using the DoNothing() procedure defined in an earlier wtf? =)

  • Anand (unregistered)

    qwerty: VB 2005 does have an IsNot operator for exactly that kind of checking

  • The_Spide (unregistered)

    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

  • Factory (unregistered)

    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.

  • Ray S (unregistered)

    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.

  • Zhaph (unregistered)
  • Zhaph (unregistered)

    Sorry, that was all a bit messy, wasn't it...

    http://weblogs.asp.net/ericlippert/archive/2004/07/15/184431.aspx

  • Tim Smith (unregistered)

    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. :)

  • Ryan Heath (unregistered)

    @Tim Smith,
    really?

    Shouldnt the compiler take care for such a case?
    Devs shouldnt try to out smart the compiler...

    // Ryan

  • Peter Gfader (unregistered)

    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...

  • Theologian (unregistered)

    <b>'This comment is intentially left blank </b>


    :-D

  • JonBoy (unregistered)

    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...

  • Miszou (unregistered)

    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 )
    ...

  • Warp (unregistered)

    "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;
    }

  • Xarzith (unregistered)

    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++.

  • SoldareDiMorte (unregistered)

    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.

  • David Crawford (unregistered)

    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)

  • Sebastian (unregistered) in reply to SoldareDiMorte
    SoldareDiMorte:
    Xarzith, that's actually incorrect TRUE is not a keyword in C++. TRUE is a MACRO defined in afx.h (#define TRUE 1)

    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

  • cbhacking (unregistered) in reply to JonBoy
    JonBoy:
    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...

    If testing a boolean variable (as in your example) then it has nothing to do with strong or weak typing, really. The return type of an assignment is the type of the assigned value, so

    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.

  • (cs) in reply to cbhacking
    cbhacking:
    An early instructor of mine would have called it bad Boolean Zen.

    Sounds like your instructor thought that "Zen" was a wild-card word -- you could stick it in anywhere and it would make sense.

Leave a comment on “Double Negatives”

Log In or post as a guest

Replying to comment #:

« Return to Article