Wednesday, 11 January 2012

ASP: some general tips


The new year has just began, and you're probably still trying to get things working after your well earned holidays. Don't worry, it won't be a too much complicated article: just a few tips not to forget when programming in ASP.

We will examine tips in no particular order, just as they poured in my mind while I was thinking about how some basic things are easily overlooked while programming. That could happen because we aren't aware of them or just because we might be lazy enough to forget about them.

Ok, take a deep breath and let's start.

Save time with includes
Includes are often overlooked, especially by beginners. Remember that they can save us a lot of time, specifically when we will have to change things afterwards. Keep your main functions and in general anything that you often use (like headers, menus or footers) on external files. Then, include those files when and where they are needed.

Use .asp as extension for includes
We are taught that includes should have an .inc extension. Forget about it and use .asp extensions, just because they will be treated as .asp file and the code will still be run on the server side. This will avoid people from seeing your code (at least at some levels).

Remember the order of execution
In an .asp file, there's a predefined order of execution for anything that the browser is loading. ASP pages are executed following this list:
  1. Server Side Includes (includes are interpreted and executed);
  2. Server Side VBScript code (anything in between <% and %> is then executed);
  3. Client Side JavaScript (finally is the turn of the JavaScript code).
This is important, especially when we try to mix server side code and client side code (maybe VBScript and JavaScript). It might happen that, for example, VBScript conditional statements are not working as expected because of that.

Use option explicit
Option explicit (<%Option Explicit%>) is used when we need to explicitly declare all variables in the page. That looks like as if we want to complicate our lives: not true. Forcing us to declare all variables, avoids variables misspelling errors. Let's say we create a complicated function that uses a variable called XMLstring. We write our code and while taken by the sacred fire of programming we type the variable wrongly as XLMstring at some point. Because the function is quite long, we will spend a lot of time finding that little mistake, while with option explicit the error is easily discovered.

Use subroutines
This is very general, but often overlooked. Use subroutines whenever you can: we do not need to write the same things over and over. We can create a subroutine and then call it when we need it. (More info here).

Use response.flush
We discussed this in the past. With response.flush we can show recordset results, for example, as soon as they start pouring from the server. Don't let the user wait while all the information is gathered from the server. (More on response.flush).

Use response.IsClientConnected
Before starting long and complicated tasks on the server, check if the client is still connected. That will avoid unneeded server workload. If we think of a situation where a user submits a server request and then goes away, being not interested in results, checking if the client is still connected will reduce unnecessary server requests.

Always close connections
Remember to close connections. And we should do it as soon as possible; don't wait and close them at the end of the page. We should open connections near the place where we need to show results; and close them just when we finish showing results. That will decrease server workload.

That's all folk for today. I hope you will still have the strength to share your thoughts about it all. If so, and I will be pleased, use the comments section below.

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.