When writing your ASP pages, there is one very important step many developers unfortunately forget to do or in some cases are too lazy to do. Always put the effort in and avoid writing sloppy code, use Option Explicit.
Option Explicit requires that all variable names be declared (with the Dim statement). It is good to declare your variables for a number of reasons. First, let's look at this code example:
MyName = "michael"
As i have mispelt MyName and written MyNam nothing will be printed out, ASP will create a new variable MyNam and assign an empty string to it. If your code is long then finding this typing mistake can take you hours.Trust me!!
Option Explicit 'Always, always, always!
'declare your variables
MyNam = "Michael"
Note that If you write the code in the style above using Option Explicit you will
get an ASP error informing you that MyNam is undefined. You must first use the dim statement. Debugging becomes alot easier!
Below is a section of code with an example of a spelling mistake. We have declared our variables but unfortunately mispelled one of our variables ie MyNam. Using Option Explicit the undefined variable will be picked up and the error message 'Variable is undefined' will be displayed. This can truely save you an awful lot of time when debugging your code.
Just note that the Option Explicit must come before any html code.
<% @ LANGUAGE="VBSCRIPT" %>
<% Option Explicit %>
<title>Using Option Explicit</title>
<body bgcolor="#FFFFFF" text="#000000">
'declare your variables
dim MyName, MyAddress, MyfootballTeam
MyNam = "michael"
MyAddress = "16 Marki Street"
The above code will produce this error message.
To sum up, using Option Explicit forces you to declare all your variables before you use them. If you use a variable you haven't declared then an error will be generated much like the one in the example above.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Web Development Articles
More By Developer Shed