Tortured Logic, Visual Basic Style

This from the Microsoft sample code for Remote Web Desktop:

sub OnControlLoad
   set Control = Document.getElementById("MsRdpClient")
   if Not Control is Nothing then
      if Control.readyState = 4 then
         Document.all.connectButton.disabled = FALSE
      end if
   end if
end sub

Not un-convoluted? No.


This doesn't seem so bad to me. I don't really know VB, but I can still read it okay. The "Not is Nothing" part comes off a bit funny. Maybe it would be better written as "if Control is Nothing then return."

The thing is, all boolean expressions are phrased in the negative. Rather than saying, "does the control exist?" the code says "not does the control not exist?". Then, rather than enabling the button, it non-disables the button.

Long ago I learned that boolean attributes should almost always be phrased in the positive, lest you wind up with twisted double-negative logic as seen here.

That's a good principal that I've never really heard put in writing before. The property of the control would better to be "enabled" instead of "disabled." This is a bad design consideration by whoever wrote the control in the first place.

It's HTML defining the "disabled" property on controls:

Still doesn't make it right.

It's stinky. VB should've had the same grammatical considerations as Ruby--human readability. The HTML part, imho, is simply bad all the way around.

Disabled is the exceptional state, but like JDC sez, you would think all HTML form objects could have enabled = true as a default. As a UI designer, I never design for this state. It does nothing but frustrate the user. Here's an element you can do nothing with. Enjoy! You could argue for discoverability or learnability's sake that it gives an obtuse hint there's something the user can do on the page to 'enable' the button. In this case, that isn't so.

You're waiting for an XHR request to return that it is interactive (and may soon be complete), something the user may have little control over. In any event, a display: none .css swap on the button would be as effective.

Why "4"? Because interactive is the 4th of 5 possible states for this object. Thanks, M$. It's my night for diatribes, since it keeps me from doing actual work!

Oh, this is fun.

I'm a little late on this and I too chuckle over the "Not is Nothing" and the default of Disabled being false instead of Enabled being true... but the argument's humor is all around the defaults our programming forefathers chose, right?

The default of a boolean is false in VB, LotusScript, and God only knows how many other languages. HTML standard features code around that assumption and would therefore have the default behaviour pivot around a boolean false since it saves the trouble of setting the default to true just to make readability better.

Poor Microsoft, IBM, W3C, etc... so much would break by changing to make me a little happier with the use of syntax and object constructs... but then...

I would be happier!

...Nah, that's a pipedream. That twisted switch is just fine when compared to sifting through some sort of crack to the brain a programmer slapped together last year in the name of "quick delivery".

I still have inherited goto's , globals, deep nested IFs, caustic Cases and On-The-Fly programming logic to bring the BP up.

Post a comment