Sunday, October 23, 2005

[geek][programming] From the "People should know better by now department"

One of the blogs in my RSS aggregator is Builder UK. Why the UK version of the site? Well, first off I think it's a better site than its American counterpart, which appears to do little more for programming-related content than regurgitate articles from the faux-Libertarian TechRepublic.com. Second, seeing as how I live and work with Microsoft technologies in the middle of the Microsoft heartland - and as I've mentioned previously, MSFT is one of our clients - I thought it'd be good to get some perspectives from outside the immediate sphere of influence of the Borg. Third, I like an international perspective on tech news just as much as I like getting an international slant on non-tech news. But they've recently endangered their position with an amazingly dumb article, VB6 Tip: Make the most of Variants. Some quick background: the 'Variant' data type in Visual Basic is an all-purpose data type. It can hold anything without choking: strings, numbers, object references. And it's a memory pig: since it doesn't know what the maximum amount of memory is that it'll need to hold its contents, it grabs a largish chunk and waits for the data to come in. True/False, requiring 1 bit of memory? Sure, you could use it for that, and eventually, at some point in the future, the variable will release all that extra memory. But not right away - you see, it could still be something else. And we don't want to give up that buffer, just in case. Now, there are some valid reasons why you might want to use such a data type - but you better have a bloody well-thought out reason for it. The Variant data type is one reason among many why so many programmers look down their nose at Visual Basic - you don't have to think as hard about what you're doing, because VB'll make guesses for you. Worse, I've seen code that declared variables as variants, and then used one or two variables as catch-alls. Need a number? No problem. A string? Sure! A Word document? You betcha! All with one variable! Whenever I helped review resumes for VB6 programmers, there were 3 things in a code sample that would instantly get a resume tossed on the No Fucking Way pile:
  • Failure to include 'Option Explicit' at the beginning of your code.
  • Remember how I said VB will make guesses for you? It'll do even better than that - if you don't require variables to be explicitly declared, it'll create them for you at run-time. A typo in a variable name all of a sudden becomes a new, legitimate variable. I've seen very expensive pieces of custom software all of a sudden break when 'Option Explicit' was turned on.
  • Failure to understand how variables are instantiated.
  • In some languages, you can declare a variable like this: "int foo, bar;" - there, you've just declared that you'll need 2 integer values named foo and bar. It therefore isn't uncommon to see this in VB6 code: "Dim foo, bar As Integer". Guess what? It doesn't work that way, but it doesn't throw an exception of any kind. bar is an integer, sure, but guess what foo is? It's a (wait for it)... Variant!
  • Indiscriminate use of Variants
  • If you haven't bothered to think through what types of data you were working with, how can I be sure that you've thought through your code? Toss, toss, toss... NFW. By itself, this article isn't enough to get me to delete my RSS feed - but they're definitely on probation.

    0 Comments:

    Post a Comment

    Links to this post:

    Create a Link

    << Home