(919) 404-9327 sales@lizardwebs.net

Use OPTION EXPLICIT

Save yourself LOTS of Classic ASP headaches - Use OPTION EXPLICIT

That just can’t be stressed enough. Use it for your own benefit. It seems like it is a pain to have to declare or DIM all of your variables, there is NOTHING worse than getting several hundred lines into a page with variables that you have setup as globals and then some that are inside functions (public and private for old scripters out there) and then discover that you’re not getting the result you are expecting. I have tried to forego the OPTION EXPLICIT before and just code on through, but it always comes back to haunt me and I end up going back and declaring all my vars with OPTION EXPLICIT.

If you don’t know what the declaration does, let me set you straight. You don’t NEED an OPTION EXPLICIT (OE) to declare your variables. Without the OE, you CAN just create a variable on the fly without declaring it. So if I need a counter variable, I can just drop it in and say For iCnt = 1 to 10 This can save a great many keystrokes. When you use the OE, you then need to go through and do something along these lines…

 Dim iCnt 'as Integer - Counter variable For iCnt = 1 to 10 ... 

*Note that the text after the apostrophe (‘) is not used as part of the processing in VBScript. I use it to track what I plan on doing with the variable only. It’s also useful if you decide to modify this script and turn it into a true COM object or class function.

Yes, it seems like extra work that isn’t needed, but when you get down into lots of variables (tens or dozens) and some of them are global (declared outside of any functions usually right at the beginning of a page) and some are private (declared inside a function and specific to that function), how will you figure out which is which? Unless you’ve got a great memory, and most people don’t, OE will help greatly – as will dropping in some descriptive information in the declaration.

Example, so you’re coding along and you seem to remember the var that you’re planning on using is called sMyLongVarName but you end up typing sMyOngVarName. Using OE, when you go to run that script, you’ll get a handy little error that tells you that the var sMyOngVarName is not found. Aha. Now if we didn’t have OE in place, we would run that script and the value of that var would be a null or empty depending on our usage. When that value was then used in a function, we would have gotten something that we probably didn’t expect. And we would seemingly have a nasty logic error that we just couldn’t figure out very easily without lots of tracing. With OE in place, we already know exactly where our mistake is. No real debugging needed.