Monday, 23 June 2008

Examples of Illogical Comments

I recently bumped into the phrase "examples of illogical comments". The moment I read that, there immediately sprang into my mind terrifying visions of code from which I thought I had escaped...

'FORM-LOAD Event.
Private Sub Form_Load()

'Put BORDER Around Main FRAME.
Call ChangeBorder(fraContainer.hwnd, BDRSTYLE_RaisedLikeAButton)

'Position RESET Button.
cmdReset.Top = Me.ScaleHeight -
cmdReset.Height - 120
cmdReset.Left = Me.ScaleLeft + fraContainer.Left + fraContainer.Width - cmdReset.Width

'Position UPDATE Button.
cmdUpdate.Top = cmdReset.Top
cmdUpdate.Left = cmdReset.Left - cmdUpdate.Width - 120

'Ensure ESC Triggers CANCEL Button.
cmdReset.Cancel = True

'Set ACTIVATE-FIRST-TIME Flag.
mblnActivateFirstTime = True

'Initialise NUMBER And DATE CONTROLS.
Call InitialiseNumberDateControls

'Clear IGNORE-COMPUTE-SUMMARY.
mblnIgnoreComputeSummary = False

'Clear INCOME-CHANGED Flag.
mblnIncomeChanged = False

'Set ARRAYS-CLOSED Flag.
mblnCurrentArraysClosed = True
mblnOriginalArraysClosed = True
End Sub

No. No, no, no, no, no! Just no.

This is an example of some genuine code that I have had the pleasure of debugging. When every line suddenly becomes three times longer than it should, it makes it a whole lot more difficult to know what's going on. Oh, how we begged, how we pleaded with its author to change his style, but sadly, that leopard would not change its spots.

Much hair was lost around the building, I can tell you ;)

So, let's just set out a few general pointers, shall we?
  • Do not over-comment. It makes the program harder to read, and significant comments can get overshadowed by insignificant comments.
  • Code should be self-documenting. A line that says "mblnActivateFirstTime = True" does not need a comment to tell us we are setting the Activate First Time flag. We can see that. If you feel it needs a comment, tell us why we are setting the flag and how it is used. Do not state the bleedin' obvious.
  • If you write a comment, write it in English. Anyone should be able to understand it. For goodness sake, certainly don't write them in COBOL as appears to have happened here ;)

Code safely. And please, don't have nightmares. ;)

Sunday, 22 June 2008

Fantastic Errors and Random Nostalgia

So, I wibbling around some random website when I click on a link and up comes an error:
Error 503 Service Unavailable

Error talking to backend

Guru Meditation:

XID: 2075015976

Oh wow! Just how fantastic is that!? :)

Best. Error message. Ever.

Ahh, it's been a while since I've seen that "old friend":

According to Wikipedia:

Guru Meditation is the name of the error that occurred on early versions of
the Commodore Amiga computer when they crashed. It is analogous to the "Blue
Screen Of Death", often referred to as a 'BSOD', in Microsoft Windows operating
systems.

The term "Guru Meditation Error" was an in-house joke from Amiga's early
days. One of the company's products was the joyboard, a game controller much
like a joystick but operated by one's feet. Early in the development of the
Amiga computer operating system, the company's developers became so frustrated
with the system's frequent crashes that, as a relaxation technique, a game was
developed where a person would sit cross-legged on the joyboard, resembling an
Indian guru. The player was supposed to remain perfectly still with the goal of
the game being to stay still the longest. If the player moved, a "guru
meditation error" resulted.

It's always great when you encounter random nostalgia :)

Sunday, 15 June 2008

Can't See the Wood for the Trees

Sometimes, you just can't see the answer even when it dangles right in front of you.

"Sign in name" and "Password" the form asked.

I entered my e-mail address and password.
The username and/or password cannot be validated, or your account is locked out or has not been approved yet.

I tried again. I failed again.

I frowned, and tried the same page in Internet Explorer instead of Opera.

I failed again.

I tried again, and I failed again. I frowned again.

I confirmed that I was using the correct e-mail address. I tried again, I failed again.

I requested a new password. I tried the new one. I failed again.

I realised that the "failed" screen said "username", whilst I had kept entering my bloody e-mail address ;)

Thursday, 5 June 2008

I'm thinkin' about my doorbell

I think I lost some credibility at work when I insisted that on the occasions that the central heating thermostat buzzed, it coincided with the times that my computer lost access to the network.
Seriously, what's up with that? I swear they always coincided, and after a few to several seconds the network would be back again...

...And / or the buzzing would have stopped...

Still, that was a different computer and central heating system ago, so is now water under the bridge (Or in the pipes?) ...

But perhaps that's why the final nail in my coffin of "He's lost it!" was sunk after I declared that my mouse stopped working when someone rang the doorbell.

I'd be sitting there, working away... And then the mouse cursor would stop moving. And then the doorbell would ring.

I'm telling you, it bloody did!

Oh, how they laughed at me.

"Enough," I decided the other day, "is enough."

It was time to prove my sanity (or lack thereof) one way or the other. So I stood outside, and I decided to ring that doorbell until my theory was proven. Or the doorbell died. Either way, I'd have proven something...

The verdict?



Yup, it turns out that of the six wireless mice in the building, two of them are interrupted by ringing the wireless doorbell.

Clearly, the other person just plain hadn't been working hard enough. ;)

So, I get one more day's reprieve from the Men in White Coats...

Sunday, 1 June 2008

Spabble

Taking a look in my Junk Mail folder, I had to chuckle

Nestling between "10 tips to getting college girls" and "Make her ooze love juice" was "Love Scrabble? Open this".

I thought for a second it may even have been a genuine e-mail of some sort, but it did turn out to be a random spam to do with some sort of "instant win" Scrabble rip-off game.

But it makes you wonder, though... Could some sort of exciting new concept game be hidden here? Could spam be combined with Scrabble to form... Spabble!

Instead of randomly picking some letters from a bag, each turn would see you be handed a selection of random junk e-mails. Perhaps the aim should be to attempt to contruct a single valid sentence from all of them. The greater the length (uh, or girth?) of the sentence and the greater its accuracy (e.g. words spelt correctly from letters with no numbers, correct capitalisation), the more points would be awarded.

It's Spabble-tastic! (Oh dear...)

Ceteris Paribus

Wikipedia tells me that:
Cēterīs paribus is a Latin phrase, literally translated as "with other things the same". It is commonly rendered in English as "all other things being equal".

Following on from yesterday's post, True and False, I felt that this seemed like a good title for something with a similar theme.

This is another bug we found in the same third-party supplier's components, and another quick sample we knocked up to illustrate the problem.
Private Sub Form_Load()

With TestControl2
   .Format = "#0.00"
   .DisplayFormat = "#0.00"
   .MaxValue = 99.99
   .Value = 99.99
   Debug.Print "Value:         " & .Value
   Debug.Print "Max:           " & .MaxValue
   Debug.Print "Value > Max:   " & (.Value > .MaxValue)
   Debug.Print "Value > 99.99: " & (.Value > 99.99)
   Debug.Print "99.99 > Max:   " & (99.99 > .MaxValue)
   Debug.Print "99.99 > 99.99: " & (99.99 > 99.99)
End With

End Sub

So, we're talking about a similar set of circumstances - we're sticking a value (99.99) and then viewing and comparing it to, well, 99.99.

One would traditionally expect, all other things being equal ;), for 99.99 to be the same as 99.99. Let's check that, shall we?

Value:         99.99
Max:           99.99
Value > Max:   True
Value > 99.99: True
99.99 > Max:   False
99.99 > 99.99: False

Ah, of course. Value is greater than MaxValue. In other words, 99.99 is greater than 99.99. Good. Right. Erm... ;)

What seems to have happened here is that, for some reason (don't ask me what), the "99.99" that we put into "Value" seems to have been artificially increased, for example, to 99.991, but when we display it it is rounded back down to 99.99.

Both this problem, and the one documented by True and False, are both perfect examples of things going wrong that just should not go wrong. Discovering that 99.99 is a larger number than 99.99 just should not happen. But it does. And when it does, you can guarantee that it'll only expose itself under some bizarre set of circumstances that don't make any sense.
After hours of following through code and feeling thoroughly confused, you eventually arrive at that place where you begin to examine the things that it wouldn't usually occur to check.

And that's when you find the little bugger that's been ruining your day. Hurrah!