ASP Coding Standards
Kenneth W. Richards
Orvado Technologies
Introduction
This document outlines the coding conventions we will be using when writing server-side scripting for Orvado and its clients. Specifically, it details coding standard and practiced pertaining to Microsoft's scripting language Active Server Pages or ASP for short.
Variables
Microsoft's standard coding practices encourage you to use three letter prefixes for your variable names. For our purposes, we will only use a single character prefix with no underscores in the name. The table of prefixes is shown below:
Prefix | |
Example | |
Uses |
a | |
aMonth | |
Array of month names |
b | |
bEditMode | |
Boolean value may be "True" or "False" | |
c | |
cPriceCode | |
Single character |
d | |
dStart | |
Date or time value |
f | |
fAmount | |
Floating point object (referred to as a double) |
n | |
nMonth | |
Numeric integer type |
o | |
oDict | |
Object created with Server.CreateObject including dictionary objects |
s | |
sFirstName | |
All string and character data (more than one character) |
For Visual Basic programmers, it is important to note that all variables in
ASP have the "variant" type. It is the variant sub-type that ASP uses to manage all of its "type checking". It is this sub-type we reference with the Hungarian
notation shown above.
Many ASP developers prefer to use the Microsoft recommended three letter prefix
for their variable names, but I find this is often overkill for the size of the
average script in ASP. There are not many sub-types commonly used in ASP
programming.
Formatting
Formatting of the source code is important to ensure readability of the code. This includes how lines of code are indented relative to one another and also how whitespace is used. What follows are guidelines to use when writing or editing code.
- Tab size is 4 characters, no more and no less
- Use whitespace between all operators (1 + x) Not (1+x). Also include spaces around all Boolean operators (x <> 5) not (x<>5).
- Use whitespace between assignment operator (x = 1) not (x=1)
- Use string concatenation operator (&) to split long lines. This is especially useful when building long SQL statements.
HTML Formatting
Along with formatting the ASP code, we also need to format the HTML code that we build since it is integrated with ASP. The following standards should be applied when creating or editing files:
- Indent all tags between <HEAD> and </HEAD>
- Indent all tags between <TR> and </TR>
- Put all tags between <TD> and </TD> including the "TD" tags on a single line (only if the line is not too long > 80 chars)
- Put tags <TD> and </TD> on a line by themselves only if the content of the tags is longer than 80 characters.
Conclusion
It is hard to get a grasp of all of the coding conventions and practices we are using. If you are new to our programming practices, you should try browsing some of our scripts to understand how the code is written and how the formatting is done. You may also find it easier to take an existing script and modify it to your own purposes. I do this all the time and it saves a lot of time. We also have templates that will help you to build web scripts.
Last Updated: Sep 29, 2003
|