Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Three Commandments for ASP Programming

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.


advertisement
f 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"
The single most common reason for ASP programmers pulling out their hair is the absence of the Option Explicit statement at the top of every ASP page. This requires you to declare a variable before using it in code. When it is not present, any variable found in your code will be assumed to be a valid variable—even a variable that you mistyped in a hurry. And that's where the problem lies: debugging a page that you know is correct but does not work properly. Can you see what's wrong with the following piece of code?

' - Get the Employee Salary curEmployeeSalary = 48000 ' - Calculate a nice 15 percent bonus on top of it curBonus = curEmployeeSalary * 0.15 ' - Add the bonus to our salary curEmployeeSalry = curEmployeeSalary + curBonus ' - Now, proudly present the total figure to the ' -- Employee who is waiting for this figure. Response.write FormatCurrency(curEmployeeSalary,2)



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.

<% Option Explicit %>



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date