Sat 13 Aug 2005
1 Introduction
This documents intends to list a number of recommendations to be used when developing ASP pages. This is not a full standards documents but a number of recommendations that should be followed.
The following areas should be extended:
§ Define a naming convention for variables. In the case of ASP it is suggested to use one of the type: sString / nNumber / objObject.
§ Define a common error handling/reporting framework.
2 Scope
The scope of this document is ASP development in the Software Development team. Specific teams may have additional requirements and standards.
3 Recommendations
1. Use meaningful names for all your variables, functions, objects etc
2. Use option explicit when possible.
3. When modifying existing code or extending it may not be possible to use option explicit due to dependencies between the files. In those cases, try to make this possible and consult your team leader if the amount of work is significant or likely to impact your schedule of work.
4. Always dim all variables.
5. Use Local Variables in Subroutines and Functions.
Local variables are those declared within subroutines and functions. Within a function or subroutine, local variable access is faster than global variable access. Use of local variables also tends to make code cleaner and avoids data getting mixed.
6. Avoid re-dimensioning arrays.
7. Use Response Buffering. This minimizes the amount of writes to the browser and thus improves overall performance. Each write has significant overhead. If some part of the page may take a long time consider using Response.Flush before very expensive operations.
8. Use client side validation when possible but validate server side as well.
9. Copy individual values from collections into local variables, if you are going to reference the value more than once. This saves ASP from having to perform lookup processing in the collection for each and every reference.
10. Don’t expect the ASP engine to clean up your objects. Always close then and set them to nothing as soon as possible. E.g. set objObject = nothing
11. If you are going to loop through a long recordset, consider loading it into an array using the Recordset.GetRows method and close your recordset before you process it. If not using arrays consider using a field reference if you are looping through many results (e.g. set objField = objRs(“fieldname”) ). Then reference with objField.value. You can also use indexes instead of field names but define the numeric values in variables. E.g. nField1 and then recordset(field1)
12. Minimize context switching. For readability and performance, try to minimize context switching between HTML and scripts. When possible, use a few large blocks of script on a page instead of several scattered fragments.
13. Handle errors gracefully. Errors should be handled and appropriate user-friendly messages displayed. Don’t assume that things will execute correctly.
14. Encapsulate your code. If you are going to have small include files with some functionality it makes more sense to encapsulate it and apply proper error handling to it.
15. Think in terms of layers. Use VBScript classes to encapsulate business logic, database access and presentation tier when sensible.
16. Limit the dependencies of objects and routines to their property and parameters. Avoid depending on global variables when possible or the QueryString and Form collections.
17. Always validate input parameters before executing code. Make sure that if an input value is invalid it is handled appropriately.
18. Before you code, think what you want to achieve and what is the best way of doing.
19. Always comment you code. Always specify dependencies in the code.
20. If you modify existing code, add comments of what you modify and when.
21. Organise and name your files meaningfully.
22. Don’t abuse on nested code include files; it makes documentation and debugging more difficult.
23. Use standard headers in all script files and use VSS auto-replace features. For example:
‘===============================================================< ?xml:namespace prefix ="" o ns ="" "urn:schemas-microsoft-com:office:office" />
‘ File: ski_snow_reports_last2_week.asp
‘ Name:
‘ Description: Main ASP page for the last 2 weeks snow reports
‘ (single and comparison)
‘ Channels: Ski
‘ Authors: Author Name 00/00/00
‘ A.N. 00/00/00: Change description
‘ A.N. 00/00/00: Change description
‘===============================================================
‘ VSS Details:
‘———————————————————–
‘ $Author: $
‘ $Revision: $
‘ $Date: $
‘ $Archive: $
‘===============================================================
1. Be consistent in your coding.
2. Always properly indent your code; it makes it more readable and easier to understand and debug.
3. Test, test and test what you do.
4. Do performance testing.
1 Further reading:
25+ ASP Tips to Improve Performance and Style
http://msdn.microsoft.com/library/default.asp?URL=/library/en-us/dnasp/html/ASPtips.asp
ASP Guidelines
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnserv/html/server122799.asp
Maximizing the Performance of Your Active Server Pages
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnasp/html/maxperf.asp
Seven Deadly Sins of < ?xml:namespace prefix ="" st1 ns ="" "urn:schemas-microsoft-com:office:smarttags" />
http://sqlmag.com/Articles/Index.cfm?ArticleID=8423
ASP Conventions
http://msdn.microsoft.com/library/default.asp?URL=/library/en-us/dnasp/html/aspconv.asp
Why GetRows is best to fetch data
http://www.learnasp.com/advice/whygetrows.asp
August 6th, 2008 at 12:37 pm
Very nice read thanks! Don’t forget to list http://www.aspnettutorials.com down on your resources list! thanks again!