How To Write Readable and Maintainable ASP Scripts (299985)
The information in this article applies to:
- Microsoft Active Server Pages
This article was previously published under Q299985 SUMMARY
This article provides some guidelines on how to write Active Server Pages (ASP) script that is easier to read and maintain. As your Web application grows more complex, it is not uncommon for ASP scripts to grow to hundreds of lines. If later you experience application errors or need to rework part of your code, it can be difficult to isolate problems if your code is complex and hard to read. This article offers best practice coding guidelines that may help avoid this scenario.
back to the topWrite Code That Is Readable and Consistent- Use consistent naming conventions.
How you name the constants, variables, and objects that are referenced in your code will determine how readable your code is. If you always use a similar prefix such as "str" for variables that contain string data, your code is easier to understand. One particularly helpful approach is to avoid using short, cryptic variable names; this is almost never worthwhile. It is far better to use longer names for the sake of clarity. You can easily follow a consistent naming scheme such as using "camel" notation (MyLongVariableName) or underscores (My_Long_Variable_Name) to make for clearer code. For detailed help on naming conventions, see the "References" section. - Format your code consistently.
When dealing with markup languages such as HTML or XML, many developers prefer that all of their the tags appear in uppercase or lowercase (for example, either <TABLE> or <table> but not <Table>), which makes them easier to distinguish from other code. You can also indent portions of the code that are logically subordinate to reveal the logical structure. For example, the following JScript function begins at the left margin, the "If-Then" clause is indented several spaces, and the code within is indented still further:
function MyFunction()
{
if(SomeVar == true)
{
Do_Something;
}
else
{
Do_Something_Else;
}
} - Comment your code extensively.
Nothing can substitute for a simple, contextual explanation for what the variable or routine is supposed to be. It helps to have both a high-level description, such as "This routine updates employees biographical info in the Employees database." It is also good to have a more technical description that includes what is necessary for this code to run, inputs, outputs, required variables, and so on. Many developers also find it useful to comment who wrote the routine and when it was last updated, although many source code control programs (such as Microsoft Visual SourceSafe) handle this aspect automatically. Usually the best way to comment a routine is to place more detailed or narrative type comments above the routine and sprinkle single-line descriptive comments throughout the routine. Below is a sample of commented code:
'Written by: John Smith
'Last Updated: 2/05/2001
'Description:
'This function updates the employees table...
Function UpdateEmployee (intEmployeeID, strLastName, strFirstName)
If SomeVar = true Then 'Add inline comments within your functions.
Do Something
End if
End Function NOTE: Remember that VBScript uses an apostrophe (') to denote comments, whereas JScript can use two forward slashes (//) to indicate comments on one line or can denote multiple line comments beginning with a forward slash/asterisk (/*) combination and ending with an asterisk/forward slash (*/) combination. - Locate variables and constants in a common place.
Rather than declaring variables and constants throughout your routines when you need them, it is easier to declare them all at the top of the routine. If you are dealing with "page-level" variables in ASP, declare them at the top of the page in a cluster. Then, if you need to troubleshoot them later, they are easy to find.
back to the topPlan Your Routines Carefully
back to the topPackage Reusable Code
back to the topTroubleshooting
There are no real pitfalls of taking the time to plan and write code that is easier to read and maintain. It may take more time initially, but it will save a lot of time in future application maintenance, especially when you need to troubleshoot problems or when the programmer who wrote the code moves on and a new programmer is left to figure out the code.
back to the topREFERENCES
For more information, see the following Microsoft Web sites:
McConnell, Steve, Code Complete, Redmond, WA: Microsoft Press, 1993.
For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
110264
INFO: Microsoft Consulting Services Naming Conventions for Visual Basic
299988 How To Package ASP Scripts by Using Visual Basic COM Components
299982 How To Create Server-Side Include Files to Package ASP Scripts
299983 How To Package ASP Scripts by Using Windows Script Components
back to the top
Modification Type: | Minor | Last Reviewed: | 7/15/2004 |
---|
Keywords: | kbCodeSnippet kbGuidelines kbhowto kbHOWTOmaster kbScript KB299985 kbAudDeveloper |
---|
|