I promise

To improve my VBA code, I promise to do (or at least try to do) the following:

I promise to stop setting objects to Nothing. It's pointless code that doesn't actually reclaim memory or destroy objects. All it does is separate your object from the memory space holding its value.

I promise to stop using Hungarian notation, at least, Hungarian notation the way everyone thinks it's supposed to look. Another recommendation that makes variables harder to understand. I did this at first because it is what everyone uses and encourages, so I assumed that it was the correct way to do things. But now it is getting in my way; much better to name your variables in a way that you can see what they are supposed to do. See Making Wrong Code Look Wrong if you aren't convinced.

I promise to use ByVal and ByRef when writing functions with parameters. A programmer should have full control over objects and not let the environment decide how objects and variables should be manipulated. VB.NET makes it easier to do this ( the IDE adds it automatically :D ).

I promise to use structured error handling instead of cramming my code with endless If statements. Trying to anticipate everything that can go wrong with code and hard-coding all kinds of error conditions leads to code bloat for anything but the simplest procedures. It also makes code harder to read, since the logic of the program is bound with the error handling code.

Now whether I actually do any of these things remains to be seen. Any additional thoughts on best practices would appreciated.

Related Articles:

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »

Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble Comments:

  1. Chandoo writes:

    I certainly cant promise all of these.. but the last one is a definite yes.

    Coming to Hungarian notation, I ended up devising my own notation after years of scripting. I have started using much more descriptive and elaborate camelCase names for significant variables in the code. When it comes to temporary values, they are almost always like tempVal1, tempVal2 etc. And finally the return values (especially in VBA UDFs) are named "retval"
    This way, it is easy to understand the code (atleast for me).

    Its been a really long time since I coded anything more than a hundred lines, so variable names really dont bother me that much… ;)

    Very nice post btw..

Note: Comments are subject to the Blog Comment Policy and may not appear immediately. To post VBA code in your comment, use code tags like this: [vb]your code goes here[/vb]

Add a Comment:

*

Site last updated: February 3, 2012