Friday, 19 August 2011

ASP: some simple advices to obtain well organized pages


When coding a new ASP page, there are some simple things to keep in mind. While our creativity shouldn't be blocked, there are some rules that - if followed - might increase the performance of our newly created page. Some of these ideas are just general advices that can be useful. Others are more imperative, and should be carefully considered almost as laws.
Anyway, let's see them one by one and then you can comment, say what you think and maybe add some other ideas that I might have forgotten.

Recordset position and related repeat regions
When executing a stored procedure or performing a query, it is highly recommended that your code is as near as possible to the place where the resulting recordset will be shown. If we are fetching a list of books to be shown on our page, the code should be placed just before – as an example – the table where the data will be displayed. Around the table, we can eventually place the repeat region. Then, just after the end of the repeat region, we close the recordset. Just to be more clear, let’s see the following structure:
1) getting the data
2) repeat region start
3) table displaying the data
4) repeat region end
5) close the recordset

Remember that the closing of the recordset could be not necessary when using a command, even if I always recommend to close the objects and set them to nothing.
The reason why I write this is because many programming software (for example Dreamweaver) have the tendency to put automaticly inserted code like recordset, at the beginning of the page, and the closing of the same recordset at the end of the page. That is a big waste of resources because the recordset is kept open for more time than needed.

Where should we put includes?
Includes should be placed where needed. Includes are one of the most powerful feature of ASP. You can use them to include entire portions of a page, replicate them, and use them wherever is needed. Includes can contain code snippets, functions, entire chunks of pages (like a menu, a header or a footer). I usually create a function.asp where I store functions, and I include it wherever is needed (normally in the head of the document). As you can see I use the .asp extension for includes, because of security reasons. If we use .inc, the file could be displayed in anyone’s browser, while an .asp page is always rendered server side.

Declare the character set
Always declare the charset! And let me add to that: always use UTF.
If you can, create an include where you put all the metas of your pages. Then include it in every page. That way, if you decide to change – for example – the charset, you can do it just once.
We already discussed the importance of declaring the charset in asp pages, so please refer to the related article if you need more info on the subject.

JavaScript is better positioned in the head of your page. Again, we can use includes, so that we can update it easily and eventually see it applied to all our pages in few clicks. Remember to declare the text type and language of your scripts.

Speed up loading time
All the above advices might increase the speed of your page. You can fine tune some of them to obtain further improvements. For example the order with which you include JavaScript libraries can really change the speed of your page (for example when loading jQuery and then related libraries).
In the past, when Internet connections were not as fast as today, programmer had to ingeniously discover new tricks to improve a site loading speed, for example using image preloaders. Today, Internet connections are faster and there’s not really the need of those tricks, however, if you want, you can sometimes use them just to obtain a desired goal (a page quite slow to load due to the amount of data displayed can sometimes benefit from those tricks).

Hope you like the above advices, and please use the comment section below to share yours.

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.