Active Server Pages |
Home > ASP > |
Always use "Option Explicit"by Rama Ramachandran As ASP programmers you must resolve to code better, more efficiently, and with performance in mind. Here are some resolutions I hope you will adopt and stick to. If you are like me, you have already broken some of your New Year's resolutions. As ASP programmers, however, it is time we made some resolutions to code better, more efficiently, and with performance in mind. Here are some resolutions I hope you will adopt and stick to. Always Use "Option Explicit"
Guess what is output from the above lines of code. You expected it to calculate 15% bonus on a $48,000 salary thereby totally to $55,200. However, what is output is the plain old salary $48,000 with ZERO bonus. What happened? A small typo. The third line of code (ignoring the comment lines) added the bonus and salary to a variable called curEmployeeSalry—Salry, not Salary. A small mistake, but when the Option Explicit statement is not used, the ASP compiler that is chugging along interpreting/compiling your code stops in its tracks and says: "You need me to add the two values and place it in a brand new variable called curEmployeeSalry. I will create a brand new variable for you." And that's what it does. It creates a brand new variable and the figure 55,200 is still sitting in the variable curEmployeeSalry, while the variable curEmployeeSalary was never modified. If you had placed the statement Option Explicit at the top of the above page, it would never complete its execution. This would give you a run time error "Variable undefined" pointing to the variable that is misspelled. So get with the program! Always declare your variables before using them, and force this discipline by using the "Option Explicit" statement.
|