For those who prefer not to worry about the techie detail of building websites, Classic ASP is a server-side script engine for dynamically generated web pages.
Classic ASP is roughly comparable to PHP or ColdFusion, and is easy to learn, but it’s not the most efficient or robust tool. It can be hard to manage and debug code, and it can be difficult to do anything beyond the basics without resorting to buying additional, and sometimes expensive third-party components.
As a result, Classic ASP has now been largely superseded by the far superior ASP.NET, which is my preferred tool. But a number of my clients still use Classic ASP, either because they have large amounts of existing code, or they prefer to stick with what they know and I’ll happily support their sites, and sometimes even build new ones for them.
Over the last few weeks I’ve built a Classic ASP application for a client. It’s the first one I’ve done from scratch in a while. It’s given me a chance to re-evaluate it as a tool, and apply some of the tricks and techniques I’ve used elsewhere to make life a lot easier. Here are some of my thoughts, not rocket science, but they might be helpful
Offload as much work as you can to the database
Classic ASP isn’t the most efficient tool in terms of server resources, so delegate as much as you can to the database. That means only retrieving the records that you need and using stored procedures as much as possible to add and update records. Not only is it much more efficient, it’s usually more secure and robust as well. It may sound obvious, but I often see projects where huge recordsets are manipulated in the web server, massively impacting on performance.
Offload as much as you can to the client
Like I said, Classic ASP isn’t very efficient, so rather than performing tasks like form validation on the server, do it in the web browser instead. Not only does it save on server workload, but frameworks like JQuery make tasks like form validation a lot easier and often, much prettier too.
Plan properly, and document what you do
When building Classic ASP applications, it can be very easy to end up writing the same code and constants in multiple pages, particularly if you don’t plan the application thoroughly in advance and end up writing code ad-hoc. You’ll find life much easier if you plan things out properly in advance, working out your constants, objects and functions, then document it all so you have a blueprint to follow. For all but the simplest applications you’ll generally save in coding and maintenance the time you spend planning, plus your successors will be hugely grateful.
Use include files to avoid repetition
Include files enable you to use the contents of a single file across multiple pages in your website. Used well, they make life much easier, as you can update your entire website by changing a single file. Not only are they great for the presentation layer, but they enable you to store constants, shared functions, and other resources in a handful of easy to access files. Which brings me on to…
Separate Presentation and Business Logic
You’ll also find life much easier if you keep the code in your web pages to a minimum and do all the heavy lifting with functions and subroutines in your include files. It should also mean that there’s less chance of a presentational change (such as a graphic) affecting the business logic. I now tend to follow the ASP.NET model, with a function on page load and one for each button press.
Use VBScript Classes
Whilst they are fairly primitive (no inheritance for example), VB Script Classes are a great way of keeping things neat and tidy.
Use Option Explicit
One of the hugely frustrating aspects of going from a strictly typed and compiled tool such as ASP.NET back to Classic ASP is that it’s not obvious if you’ve made a typo in a variable name. Enabling Option Explicit will help a lot, as it forces you to declare every variable used and will alert you to any typos.
Be careful with your datatypes, and if in doubt, check and cast
Because VBScript is a loosely typed language, it’s easy to end up in a situation where you are trying to multiply a string. To do this, make sure that you check variables using functions such as IsNumeric, and cast them using functions such as CInt and CDbl.
Check for Errors
Far too often I see Classic ASP pages where developers just haven’t allowed for potential errors and a simple problem could result is a user seeing a nasty error message. Yes, error handling in Classic ASP is pretty basic, but there are ways around it, check this article out for more details.
Build-In Testing and Debugging
As with most programming projects, you’ll find life much easier if you build in tests and debug messages as you go along, which you can then use to test the impact of any changes, and figure out the cause of any problems. There are also a few test frameworks such as ASPUnit (a port of JUnit) out there.
As for the project, I’m not really allowed to talk about the specifics, but it was a series of websites that enabled customers to redeem a voucher and enter themselves into a daily prize draw, as well as conducting the draw and notifying entrants of results. The first promotion ran for a month, and there were over 10,000 entrants a day. It was quite a high profile promotion but ran really smoothly, and the client was really pleased.
