Why is Option Explicit optional?

  • Everywhere I look, advice is that declaring of variables is a good thing and that use of Option Explicit is highly recommended.


    What I can't fathom out is why is it optional? Is there any down side to using it?

  • Re: Why is Option Explicit optional?


    It is an EXTREMELY good thing and i pains me to NOT see it in code.


    The fact is undeclared variables are cast to variant types, so in terms of getting something working really really fast and take a slightly lazy approach, MS have allowed this to happen (they are allowing users to not think or be concerned about what variables they are using, or what type they should be).


    I always use it. And I have it on by default. In the VBA editor, click on Tools menu / Options / Editor / check on Require Variable Declaration.


    This injects Option Explicit as and when required.


    There are arguments to be had that Variant data types are quicker to process/manipulate by the VBA engine, but I have seen no evidence of this, and whatever speed improvements there are, it would be insignificant for 99% of code written and when weighed up against the time lost by tring to understand why x1 = 0 doesnt work, because it should be xl = 0, I would implore everyone to use Option Explicit


    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Why is Option Explicit optional?


    I was asked this question on another site (similar question by another person - not a crosspost!) - this was my answer:


    [hr]*[/hr]


    If you Dimension a variable you are letting the compiler know that you are planning to use a certain amount of memory in advance of that variable being used. In most VBA programs on modern machines this wouldn't create a huge performance benefit, but is widely considered best practice.


    One of the reasons that it is considered best practice is the auto-capitalization feature in the VBE which can help you as you're coding and debugging to spot any issues.


    If you dimension a variable, and do the following:

    Code
    Dim hWnd
    
    
    hWnd = 1234
    '// some more code....
    hwnd = 2345


    The VBE will automatically capitalize the 'w' in the variable name and change it to 'hWnd' - this can act as a small reassurance that you've got the right variable and not created a typo.


    If you don't dimension a variable, you could easily find yourself in this kind of scenario:


    Code
    hWnd = 1234
    '// some code here
    hwnd = 2345

    In this scenario, the VBE will actually change the first occurrence of 'hWnd' to 'hwnd'. We then go on further to create a typo of 'hnwd' and now our code looks like this:


    Code
    hwnd = 1234
    '// code here
    hwnd = 2345
    '// code here
    hnwd = 3456


    And suddenly you're scrolling through lines of code trying to find out why you're getting unexpected results.



    TO BE SAFE

    1. Use Option Explicit
    2. Declare all variables As their respective data type or object. This also gives the added benefit of IntelliSense


    [hr]*[/hr]


    In addition to this, as Ger mentioned - if you do mistype something it will let you know that you have a variable in there which hasn't been defined. Option Explicit doesn't serve the same purpose it did years ago when memory management was much more of an issue in programming, but it still serves a very important purpose in your code - especially if you're still learning the ropes!

  • Re: Why is Option Explicit optional?


    At the risk of playing devils advocate... ;)


    There are always reasons for and against most cases for all things .. and in this case.. it would NOT be disadvantageous to use Option Explicit in any situation.


    SO touched on a good point.. memory issues really don't play a part in the against vote nowadays (for many people).. hey.. I got a new rig.. what do i care.. etc etc...


    Regarding Ger's point about MS allowing users to taking a 'lazy approach' by allowing them to not declare their variables (therefore automatically assigning them as Variants).. mm.. I am not sure if that or relying on intellinsense is more 'lazy'..?


    I will totally agree that when finished.. code that is Dimensioned correctly is easier to read/follow/update for others.


    I have seen many codes that run like dogs (but are dimensioned correctly) and codes that run very fast (and are not dimensioned correctly)... and.. all that vice versa..


    Probably best to write good fast efficient code as a priority, imho.. but I am not an expert (hope to be one day)..


    Good question/post btw.. :)

  • Re: Why is Option Explicit optional?


    Write code as well as you possibly can but for goodness sake, for all concerned, DECLARE your variables. It does make your code easier to read. If you are not planning on being the custodian of a file for the term of your natural life for the love of god give the poor souls that pick up your file a fighting chance to understand what you are trying to do. Using Option Explicit is like creating a management summary for all who gaze upon your book of code. Happy coding!!!


    Take care


    Smallman

  • Re: Why is Option Explicit optional?


    I'm a self taught VBA coder and when I first started it was hard enough fixing errors when Option Explicit wasn't in use. There was the occasional typo of my own doing, but it was mainly trying to figure out what all the different Dim declarations actually meant. ie, Long for numbers, Double for really big numbers etc etc. I often used to get them wrong or mix them up & many a time I thought of saying "to h*** with it" & use Variant for everything. Eventually it all sank in & now I use Option Explicit by default, it makes life so much easier, but not understanding the basics like ranges, objects etc made debugging a nightmare for me

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!