Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

ASP Coding Tricks

Rated 4.06 (Ratings: 2) (Add your rating)

Log in to add a comment
(7 comments so far)

Want more?

 
Picture of mwarden

Matt Warden

Member info | Full bio

User since: May 18, 1999

Last login: September 25, 2007

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<br>&nbsp;&nbsp;&nbsp;&nbsp;.method<br>&nbsp;&nbsp;&nbsp;&nbsp;.property = value<br>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...

Submitted by sgd on November 22, 1999 - 10:43.

You did note that they may not be performance-friendly, so you covered yourself, but using "With ..." is *very* expensive. If its a lot of html you have to output, go with blocks of html outside of the ASP.

Also, just so the real VB(Script) newbies out there don't get hung up, calling a sub in VB(Script) doesn't take parentheses, so the above calls should be:
P "&lt;table&gt;"
instead.

Good tips though, me likes em. --The "Stuffing" one should be second nature, you hear that readers?? ;)

login or register to post comments

Submitted by Ratface on November 23, 1999 - 03:33.

The "P("print this")" example is certainly a great timesaver for the person who writes the code in the first place. I would warn however that it makes your code that much harder to read for anyone following in your footsteps. If you decide to use such a shortcut, be certain to document it well - and try to use it consistently - otherwise you could find yourself being nominated for obfuscated code contests :-)

login or register to post comments

Submitted by gian on December 10, 1999 - 07:50.

Uh..not really SGD...If you have time to look into core C++ object instances, you'll notice that by using 'with', you use only one instance of the object, that is (at least) a GREAT memory and machine time saving...

login or register to post comments

Submitted by cmcphate on April 28, 2001 - 22:35.

Excellent tips. This has made my code much easier to read.

login or register to post comments

Gian--

Submitted by sgd on April 29, 2001 - 15:16.

yes, there's only one instance of the object created, but each property set is a round trip to that object, which isn't cheap. So the example above would be six round trips to the response object....

login or register to post comments

What's the diff?

Submitted by mwarden on April 29, 2001 - 15:40.

How does that differ from using Response.Write outside of a With construct? In that case, if I'm understanding things correctly, you're making six trips to six different instances of the Response object, no?

login or register to post comments

mwarden...

Submitted by gian on May 4, 2001 - 01:23.

Correct. Withouth 'With' you'll actually using 6 fake pointers (that is 6 different pointers to the same object) that won't be easily freed. Outside a with construct, the parser engine will need to copy the instance of response into a new pointer every time. Now i don't want to think about how the engine will manage to free a pointer to an object wich has not yet been destroyied....(ouch)

login or register to post comments

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

evolt.orgEvolt.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.