Asp Coding Tricks
Posted on 21 Nov 1999
by Matt Warden (mwarden)
Rated 4.07 (Ratings: 2)
- More articles in Code
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.
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:
.property = value
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).
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 & vbCrLfEND SUB
This adds a newline character so that the HTML is easier to read.
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
strFormfield = trim(request("formfield"))IF strFormfield <> "" THEN Response.Write strFormfieldEND 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?
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.