bassrek
Feb 2 2004, 07:39 PM
Here are some general tips and observations I've come up with with regards to ASP programming.
Option Explicit
Always begin your ASP file with <% OPTION EXPLICIT %>. By adding it, you are required to declare all variables, and declaring variables is a good thing! How many times has this caused a problem?
| CODE |
theloop = 0 do while theloop < 10 response.write theloop & “<br>” thelopp = theloop + 1 loop
|
If you ran this code without option explicit, it would result in an infinite loop, and cause you unneeded frustration trying to figure out why. If you take the time to declare option explicit, the page would break before executing the loop, letting you know that “thelopp” is an undefined variable.
Don’t use .inc
That’s not to say, don’t use includes. There are many standard include files we use as developers. You have the option to save them as .inc files and include them into your code. Do not do this! .inc files will be displayed as raw text when requested by a browser. Crafty web surfers can guess common file names, such as connect.inc and sensitive information like username and password are stored in these files, not to mention your own code which is now available for users to steal. Create a system for your includes. Use a common prefix or suffix, or store them all in a common directory. But whatever you do, save them as .asp files. They can be included, and they will not be rendered as plain text to the user.
Indent
Hopefully you already have an HTML indenting system. Creating an indenting system for your ASP code will save you headaches in the future. There are no set in stone rules, but there are common indention conventions for if..then statements, loops, select..case blocks, and so on.
| CODE |
select case theVar case 1 if session(“User”) = “Mike” then response.write “Hello, Mike” elseif session(“User”) = “Jeff” then response.redirect “logoff.asp” end if case 2 response.write “Go away” case else newVar = “3” response.write “Who are you?” do while newVar <= 4 response.write “Hello<br>” newVar = newVar + 1 loop end select
|
The code doesn’t make too sense as far as what it’s meant to achieve, but beyond that, it extremely unreadable.
| CODE |
select case theVar case 1 if session(“User”) = “Mike” then response.write “Hello, Mike” elseif session(“User”) = “Jeff” then response.redirect “logoff.asp” end if case 2 response.write “Go away” case else newVar = “3” response.write “Who are you?” do while newVar <= 4 response.write “Hello<br>” newVar = newVar + 1 loop end select |
The code is still screwy, but at least it can be easily read!
Plan your projects
When you start your next project, don’t immediately go to the computer and fire up your favorite ASP editor. Sit down at your desk with a piece of paper and pencil. Writing down project goals, writing the functions and routines in English (or your native tongue), stepping through those tasks with pseudo code BEFORE writing any actual code will save you time in the long run. Try it out once, you’ll be surprised how handy those original design notes come in handy as your project progresses.
These are some general observations. Hopefully they'll help you in your projects.