Does not equal



  • I managed to find this little beauty of code in a batch at work
    today.  iBilocRenewCommType could be a value between zero and four
    and the idea was to enter the if block if it didn't equal the override
    rate code, which was one.  guess what happens when
    iBilocRenewCommType equals two instead of the more usual zero or one?



    <font face="Courier New" size="2">if (!iBilocRenewCommType == BILOC_RENEW_OR_LEVEL_COMM_TYPE_OVERRIDE_RATE) {...</font>



  • @firewireguy said:

    I managed to find this little beauty of code in a batch at work today.  iBilocRenewCommType could be a value between zero and four and the idea was to enter the if block if it didn't equal the override rate code, which was one.  guess what happens when iBilocRenewCommType equals two instead of the more usual zero or one?

    <FONT face="Courier New" size=2>if (!iBilocRenewCommType == BILOC_RENEW_OR_LEVEL_COMM_TYPE_OVERRIDE_RATE) {...</FONT>

    Doesn't '!' bind tighter than '==' ??? I am assuming this is C or C++, as !<int> would not compile in Java. !0 = true, !1 = false. I *vaguely* remember that !<anything other than zero> is false. Assuming I remember C correctly, if it was 2, this would be !2 == 1 or 0 == 1  --> false (would not execute theblock)..or am I getting senile?

     



  • That's a poster child example of why ad hoc testing doesn't cut it. While I guess it could be a true logic-WTF, it seems more likely it was just a typical case of the fingers not doing what the brain intended (that everyone makes occasionally), and 99.9% of the time is picked up by the compiler or becomes immediately obvious at runtime.



  • @snoofle said:

    @firewireguy said:

    I managed to find this little beauty of code in a batch at work today.  iBilocRenewCommType could be a value between zero and four and the idea was to enter the if block if it didn't equal the override rate code, which was one.  guess what happens when iBilocRenewCommType equals two instead of the more usual zero or one?

    <font face="Courier New" size="2">if (!iBilocRenewCommType == BILOC_RENEW_OR_LEVEL_COMM_TYPE_OVERRIDE_RATE) {...</font>

    Doesn't '!' bind tighter than '==' ??? I am assuming this is C or C++, as !<int> would not compile in Java. !0 = true, !1 = false. I *vaguely* remember that !<anything other than zero> is false. Assuming I remember C correctly, if it was 2, this would be !2 == 1 or 0 == 1  --> false (would not execute theblock)..or am I getting senile?

     


    Correct!

    To be fair this was written as a one off batch so it's not like it ever was heaviliy tested or have been thrashed in production.  But still!


  • Was this code ported from VB by someone unaccustomed to C-style operators?

    I mean, in VB you have the:

    If Not something = Somethingelse Then

    which has a C equivalent:

    if (something != Somethingelse) {

    And is decidedly different from:


    if (!something == Somethingelse) {



  • Here's something against first-look intuition.

    When compiling with dev-cpp  -Wall -pedantic this sample:
    #include<cstdio>
    int main(){
      int x;
      if(!x==7)
        puts("lol"); 
    }

    It gives no warnings at all, hover, when changed to "more correct" :
    #include<cstdio>
    int main(){
      int x;
      if(!(x==7))
        puts("lol"); 
    }

    It throws a warning.

     

    (:P this is due to uninitialized variable detection, which has no importance when comparing !x with 7, but kicks in when x is compared to 7 )



  • @firewireguy said:

    I managed to find this little beauty of code in a batch at work
    today.  iBilocRenewCommType could be a value between zero and four
    and the idea was to enter the if block if it didn't equal the override
    rate code, which was one.  guess what happens when
    iBilocRenewCommType equals two instead of the more usual zero or one?



    <font face="Courier New" size="2">if (!iBilocRenewCommType == BILOC_RENEW_OR_LEVEL_COMM_TYPE_OVERRIDE_RATE) {...</font>
    This is one of those reasons I much prefer Java's strict separation between numeric and Boolean types.  It catches things like this in the compiler rather than at runtime in an edge case.



  • Yes, VB does use <> to mean not-equals.  The Not keyword (or operator) is usually used with Is, as in:

    If Not SomeVar Is Nothing Then ...

    ...which means that the variable SomeVar has a value.  (Uninitialized variables have the value Nothing.)

    I would never use Not with =; Not is generally used with Is.

    VB.NET 2005 now has the IsNot operator, to avoid the silly-sounding "If Not X Is Y" construction.  You now can say "If X IsNot Y".

     



  • Isnot sounds like some Sumerian boy's name.

    [i]a mother's call resounds across the plains[/i]

    ISNOT!
    GATHER THE GOATS!
    IT IS TIME FOR YOUR CUNIEFORM LESSONS!


Log in to reply