Global variable parameter WTF?

So having decided you are going to ignore most published best practice and scatter global variables through out your code, why would you then selectively, occasionally pass them to certain routines as parameters??

Just to make maintainers lives hard?

[module declarations section;]

Dim Value

[500 lines later in the middle of some routine:]

prepare dest, Value, operation

[a thousand lines later:]

Sub prepare (target as string, theArray, add)

Prepare can already see that global variable ‘Value’ (a type would have been nice (its actually a variant array in this case) so why pass in a further reference?

Inside prepare we can now access that global 2 ways via the ‘theArray’ parameter and directly via ‘Value’.

Can anyone suggest when/why this might be good? (I can’t)

[This reminds me of the old ‘assume no aliasing’ VB6 optimisation that I was always too scared to use]

I managed to do that update I was working on, just need to script it into a patch and see if I can get it stable enough to release.




12 Responses to “Global variable parameter WTF?”

  1. Martin Rushton Says:

    Isn’t using “value” (a property) as a variant somewhat dodgy too?

  2. Simon Says:

    yeah poor choice of name too, actually maybe it was ‘values’ in the original code. I’ll check

  3. tfsjohan Says:

    Global variables (except constants) are pure evil! You never know when and where its value changes and it’s really hard to read and maintain.

    I think using ByRef-parameters to have multiple return values are evil too. Function can return both objects and values, so why not be a good boy and create a custom type or object? But MS seems to like it since the Office OM is full of them… Even in new interfaces like the document inspector interface. Why?

    And static variables inside a sub? Nightmare!

    Wouldn’t it be cool with a compiler that actually could give you some sort of punishment, like an electric chock, when ever you code in a really bad way? :)

    // Johan

  4. Harlan Grove Says:

    Internal static variables are handy when saving state information across possibly many recursive calls. Or would recursive calls be added to the BAD PRACTICE bin? To anyone with functional programming language background that’d seem kinda harsh. I know how to avoid using internal static variables by using optional arguments to pass state information to recursive calls, but in my experience that doesn’t improve reliability. Is there some better way?

  5. Simon Says:

    Agreed on all counts, although it would be even neater if the feedback compiler could feedback to users for messing around, and project managers for unrealistic expectations.

    On modifying parameters, the whole of the win32 API works that way (and COM), the return value is success or fail or error value (eg HRESULT), and the effect is seen in modified passed in parameters. Dunno if there is a better workable approach?

    [I agree in your own code multiple returns is a sign of needing objects]

  6. Simon Says:

    I only use Statics at the module level rather than inside routines. I’m not particularly sure thats better, but I find it easier to keep track that way.
    I use recursion and don’t have it in the ‘BAD’ bin.

  7. tfsjohan Says:

    Harlan, yeah I think I’d prefer arguments and return values in recursive methods.

    I agree it sounds kind of harsh, my bad… Maybe I should have used big ironic smileys after each paragraph. :)

    Simon: I think returning objects or strucs/types and throwing exceptions would be a better approach. Unfortunatly VBA Class Modules are very basic and kind of hard to work with, so I can understand why so few VBA developers fo this.

    I come from a background in web development with .net and javascript and one of my best friends is a PM for the BEA Java Virtual Machine, so OO is what’s natural for me. And I guess you like what you know. But then again, there must be reasons why all major programming languages that are widely used today are object oriented…

    // Johan

  8. tfsjohan Says:

    And yes, I think recursive methods are excellent… :)

  9. Chris Says:

    Not defending globals here, but I suppose you might want to do this if you had written, say, an InStrRev routine (yes, I know it exists now) or some other generic handy function and wanted to pass a global to it in this instance. I’m sure I’ve done what you’re complaining about in the past.

    If “prepare” is only called once then it’s a bit curious.

  10. Marcus Says:

    “…some sort of punishment, like an electric [s]hock…”

    I’m glad I’m not the only macabre one, Johan. In a prior contract I joked with a colleague that we needed to figure out a way to send 50,000 volts through the mouse every time a user did something they weren’t supposed to.

  11. Ross Says:

    I think Globals are a often sign of laziness, I just looked back at some code i am writing now and see that I’ve got no fewer than 9! 5 of which are Const, which i think are fine (titles and a few hard coded file extension strings), of the other 4, only one could be augured to be worthwhile, the others would be better at proc level, I think i will change them latter…..! and all this is in VB.Net, when we don’t even need to use globals!

  12. Simon Says:

    Fair point Chris
    I guess you then at least de-couple that routine for easy reuse.
    I actually wonder whether the stuff I’m seeing is a previous maintainer improving from undeclared variables.

    Johan I havent expressed a preference for any style of coding I merely pointed out the way COM and the WIn32 API work. That would go some way to answer your question of why MS like this approach so much. We’ll have to agree to disagree about the importance and universal-ness (and one-and-only-true-way-ness) of OO languages.

    Ross I like to think of it as efficiency rather than laziness.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: