[thelist] Friday Freebie

Scott Dexter sgd at ti3.com
Fri Oct 13 16:02:24 CDT 2000


After a week of whipping other developers' code into shape, one of the
things I came up with (and had to deliver) was code conventions and list of
do's and don'ts. Some of them are performance related, some are readability
related, some scalability/reuse, and some were just stuff from The Way Scott
Writes Code(tm) (the book will be out just in time for Christmas) or
specific to the ways we have things set up here. At any rate, because I'm
not 100% today, I'm pruning the do's and don'ts and sharing with you kids.
Aren't you lucky ;)

<tip type="general ASP coding techniques" author="Scott Dexter"
email="sgd at thinksafely.org">

--do's and don'ts--
DO
* be consistent in methodologies. For example, if you use one way of moving
from page to page, don't change in the middle of the site --this makes it
tough to debug and maintain

* that being said, DO return to the same page when submitting forms, this
way you can catch and display errors much nicer, and *then* redirect to a
results page

* use Application scope variables for _static_ values, like System ID, DB
connection strings, arrays of days of the week, etc. --Think of them as
constants

* consolidate include files, even if some of the functions inside won't be
used. IIS' caching engine will keep pages of 100% ASP (no HTML) cached in a
pre-compiled state, so its not a performance issue --consolidate javascript
includes as well, and use an ASP include to include them, as some 
browsers don't understand <script LANGUAGE="JavaScript"
SRC="../tools/isnumber.js"></script>

* use the connection object to execute SQL statements, like so:
	<% 'returns a recordset
		set oRS = oConn.Execute(SQLStatement)
	     ' doesn't return a recordset
		oConn.Execute SQLStatement %>

* pass parameters by value (sub DoSomething  (byval x, byval y) --the
default in VB and VBScript is to pass by reference, and that can be
dangerous

* reuse connection objects (no need to open 3 connection objects if you can
avoid it)

* create your objects AS LATE AS POSSIBLE (for example, don't create a db
connection at the top of the page in anticipation of using it, create it
instead right before you use it (hopefully inside a function, wink wink))

* close/set to nothing your objects AS SOON AS POSSIBLE (like the next line)

* use clean, user friendly error trapping, and log the error to a file; send
of an email

DON'T
* use a sub/function just to save typing. MAKE ROUTINES COMPLETE OPERATIONS.
Think of subs and functions as stand-alone pieces --they should produce
complete results regardless of where they're called from. If you have a
function that opens a recordset, manipulate that recordset inside the
function and return results. DON'T just open the recordset to a global
variable and exit the function. This is horrendous to support and debug, and
really not all that reusable

* ignore Netscape. This has the penalty of dismemberment. You'd better make
sure your stuff works with NN4+ and IE4+ --including javascript, CSS, and
dHTML. --If you can't make it work in both, find another way. Oh, degrading
gracefully to NN3 and IE3 would be nice, too.

* use <%@ Language = VBScript %> --its the default language and declaring it
is a performance hit

* use Session_OnEnd --it is nowhere near reliable

* put open code segments in include files. if it needs to be in an include
file, make it a function, then put it in an include file

* use On Error Resume Next until you're 99.99% done with testing

* use the public keyword; it doesn't mean anything in VBScript

* use global variables in include files, this only leads to confusion

* create Connection objects unless you explicitly need them for something
(like changing the connection properties)

* explicitly create Recordset objects (see connection execute example below)

* create a second recordset when you can grab what you need from the first
one and close and reuse it

* wait until the end of the page to close/set to nothing your objects, get
rid of them as soon as you can

</tip>


sgd
--
work: http://www.ti3.com/
non: http://thinksafely.org/




More information about the thelist mailing list