Saturday, 31 May 2008

True and False

Normally, we would expect something to be either True or False (techy note: I said normally. I'm ignoring special circumstances such as Null).

Here, however, is a perfect example of something completely illogical that is perfect as a first example for this blog. We bumped into this little oddity a few years ago at work during my main day job as a Visual Basic 6 programmer.

Something I would like to achieve with this blog is to allow even non-techies, non-computer people, non-programmers, a chance to understand what I'm talking about. Now, I won't always be able to do this, but I'm hoping that this post can be an example of one that can be understood by someone who can be logical, even if they don't know the first thing about programming.

On that note, here's some VB6 program code ;) We found this bizarre bug in some components written by a third-party supplier and wrote this sample to illustrate the bug to them...
Private Sub Form_Load()

TestControl.Locked = True

Debug.Print "TestControl.Locked", TestControl.Locked
Debug.Print "Not TestControl.Locked", Not TestControl.Locked
Debug.Print "TestControl.Locked = True", TestControl.Locked = True
Debug.Print "TestControl.Locked = False", TestControl.Locked = False
Debug.Print "TestControl.Locked <> True", TestControl.Locked <> True
Debug.Print "TestControl.Locked <> False", TestControl.Locked <> False
Debug.Print "CInt(TestControl.Locked)", CInt(TestControl.Locked)
Debug.Print "CInt(True)", , CInt(True)

End Sub

Now, what we've done there, is set something to True. We've then printed the value of the thing we set to True in various ways - specifically, we've asked, "What is it?", "What isn't it?", "Is it True?", "Is it False?", "Is it not True?", and "Is it not False?", and (a techy one) "What is the behind-the-scenes numerical value of it?"

Here's what we got:
TestControl.Locked          True
Not TestControl.Locked      True
TestControl.Locked = True   False
TestControl.Locked = False  False
TestControl.Locked <> True  True
TestControl.Locked <> False True
CInt(TestControl.Locked)     1
CInt(True)                  -1 

So, what does that tell us?

Well, it tells us that:
Yes, it is True. Yes, it is not True. It's equal to True. It's equal to False. It's not equal to True. It's not equal to False.

Got that? ;) No, neither had we ;)

I told you it would be illogical ;)

The Science Bit
We can actually get some insight as to why this happened thanks to the final two things we thought to try in our test program. The CInt() function (Convert to Integer) forces our True/False value to be displayed as a number (i.e. an Integer)

True and False typically equate to 1 and 0, on and off, yes and no.

Visual Basic 6, however, treats "-1" as "True" (as we can see from the last line above) and everything else as "False". We can see that, rather than being "-1" as we would expect the value of the Locked property to be, it has actually been set to "1". This is likely to be a bug inside the third-party component where "True" has been treated as "1" at some point, and it's somewhere after that that the world has stopped making sense...

Tuesday, 27 May 2008

Illogical Operators

Logical operators are everywhere.

Even if you aren't "a techy", you use them all of the time without even knowing.

They are the "glue" that binds decisions, from those made by computer programs to those made in every-day life.  They are the "ANDs", the "ORs", and the "NOTs" that allow decisions to be made.

"If it is raining and I do not need to go outside, then stay inside."

If these two things are true, then we've got something to do.  These are "conditions" that evaluate to TRUE or FALSE.

Let's take an every day computer example - getting a tenner from a bank machine, hole-in-the-wall, ATM, or whatever you prefer to call it.

There will be some of that computer programming gubbins that says "If account balance is greater than ten pounds and PIN is valid, spit out ten pounds."

In code, this could look like this:

If AccountBalance > 10 And PINIsValid Then SpitOutTenPounds

Seriously, it really is that simple.

Little things like this make sense.  They are all around us, we use them all the time, we take them for granted.

Sadly, the job of a "techy" seems to deal more in the realm of "illogical operators".  Logically, I guess that this must be the reverse of a "logical operator".  Whilst the term popped into my head all of its own accord, as I write this entry, there is barely a page of hits on Google.  Hey, that's pretty good though ;)

Illogical operators.   The things that do not make sense.  When you add 1+1 and arrive at 3.  It does not make sense.  It cannot be true.  But all too often, the available information points to the fact that it is.

This is just one of the many types of nonsense that this blog aims to share with the world...