Skip to page content or Skip to Accesskey List.

Work

Main Page Content

Asp Coding Tricks

Rated 4.07 (Ratings: 2)

Want more?

  • More articles in Code
 
Picture of mwarden

Matt Warden

Member info

User since: 18 May 1999

Articles written: 7

This article will expose a few tricks that can be used to speed up the ASP development process. The focus of this article is not performance, even though some tricks will actually improve load time and server stress.

With Response

If I ever see With used in an ASP script, it's usually dealing with a server object. Let it be known that it is not against the law to use With on ASP's objects. Do you get tired of typing "Response.Write" every line? (Because you wouldn't dare switch between <%  %> just to add content to the HTML stream, right?) Well, would you rather simply type ".Write"? Then you might want to try this:

<%

' Begin the With

With Response

.Write "this "

.Write "is "

.Write "the "

.Write "same "

.Write "as "

.Write "Response.Write"

' End the With

End With

%>

With statements have the syntax of:

With Object
    .method
    .property = value
End With

Of course, each method or property does not have to be the same. The above '.Write' example was just one use (and not a very good one at that -- the usefulness would be more apparent if there was more text/code).

P("print this")

What's that, you say? Not good enough for you? Well, how about creating a custom subroutine:

<%

SUB P(string)

Response.Write Replace(string, vbtab, "")

END SUB

P("<table>")

P("    <tr>")

P("        <td>")

' and so on...

%>

And you could even edit the subroutine to add HTML-debugging aids. Here's my favorite:

SUB P(string)

Response.Write string & vbCrLf

END SUB

This adds a newline character so that the HTML is easier to read.

Stuffing

No, this has nothing to do with that delicious Thanksgiving side dish. It has to do with stuffing certain values from collections into local variables. For instance, rather than using:

IF trim(request("formfield")) <> "" THEN

Response.Write trim(request("formfield"))

END IF

use this:

strFormfield = trim(request("formfield"))

IF strFormfield <> "" THEN

Response.Write strFormfield

END IF

This is also quite resource-friendly. Not only is grabbing the same value from collections resource expensive, but using the same function multiple times is not necessary and also spends resources. Besides, it wastes valuable keystrokes... and we're lazy, right?

Included Files

You probably think I'm going to say something obvious like: Use include file to reuse code. Well, I am. Included files allow for easy updates and maintenance of frequently-used code. It also helps with debugging. Also, put the code in subroutines and functions. This way, you can put all of your included files at the top of the script in one place. Then, call the functions where they need to be called. This eases management of files, especially when you have a 2,000-line script.

For more information...

msdn.microsoft.com
4guysfromrolla.com
15seconds.com
www.learnASP.com

Matt Warden spends his spare time writing up author bios for his accounts on various websites... er... you know what? It's all here somewhere anyways. No use repeating myself...

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.