Excerpted from CF Advisor, February 1999.

Take Only What You Need:

We've all been taught that, when we have a choice to take all we can of something, we should instead take only what we need.

That's a good lesson to remember when coding SELECT statements in SQL, as it's easily overlooked. When we're looking for the records that meet a given criterion, we may focus carefully on the WHERE clause to determine which rows to get, but we may get lazy and request "all" the columns for each row, as in:

SELECT * FROM xxx

If we only need a count or a single or small number of columns, it's far better in those cases to specify just the column(s) we seek, such as:

SELECT contactid, contactname FROM xxx

This may seem an obvious suggestion, but it happens quite often. And the effect of choosing the more limited set of rows can be dramatic. In tests, this has led to a significant reduction in execution time for queries. This will depend on the size of the recordset as well as the amount of data in each row. Since the SELECT * pulls all the record's data into the server's memory for processing, the greater the number (or larger in size) the records are, the more dramatic this reduction will be.

Reductions of as much as 90% can be expected. Which means a query that took 5 seconds might now take just 1/2. In interactive applications, that's substantial! And a reduction of 1800 milliseconds to 180 could make a huge difference in a request that's processed thousands of times a day. Sometimes, it's the little things.

Turning on Line Numbers in Studio

You've probably noticed by now in Release 4 that when CF reports an error it shows the error number of the line causing the problem. Fortunately, that makes it very easy to hop into Studio and find the error. But you'll find it easier if you have line numbers displaying in Studio (though you could also simply use the "Edit>Goto Line" command.)

It's a two-step process. Each involves pressing a button in Studio's "editor toolbar". This is the vertical row of buttons in Studio's display to the left of the text area holding your code and to the right of the resource tab.

There is a line number button that turns on line numbers (it's represented by a pound sign icon in the editor toolbar). But that's not enough. You must also tell Studio to show the gutter. This is done with the button just above that which appears as a box with a left arrow off of it.

Having turned both of these on, Studio will now show the line numbers for your template which will map to the error line numbers in CF Server error messages.

Unfortunately, if you have any CFINCLUDEs in your template, the server will report the line numbers as if the included code was part of one large template. No studio solution to that, just yet. Also, in 3.1, if you have word wrapping turned on in Studio (the button above the "show gutter" button mentioned above), the line numbers can get a little confused. This problem is corrected in 4.0.

Testing Max Length in HTML TEXTAREA fields

There is no way in HTML (currently) to indicate a maximum length value for data to be entered in an HTML TEXTAREA field, such that the typing is halted at that length. The INPUT type="text" form element has a "maxlength" attribute to do this, but not TEXTAREA.

You can, however, use Javascript to test when the user leaves the field. This may be a little less annoying than their getting an error from a server-side error check. Try:

<textarea name="description" rows="10" cols="50" wrap="virtual" onChange="if (this.value.length > 2000) {alert('Sorry, Description must be limited to 2000 Characters. Following are the first 2000 characters:\n'+this.value.substring(0,1999))};">
#Description#
</textarea>

Of course, this will only work on a client that supports Javascript, so you'll want to back it up with a test in the server as well. Keep in mind that there are any number of possible ways the form may not execute this "onchange" test, such as if data is pulled in from a database and presented to the user in this textarea, but they don't change it. If it were already longer than you expect as stored in the database, you'd still want to catch it. You could do this by also checking on the submission of the form (onsubmit).

Tips Contents:

| Home | ColdFusion | Articles | Presentations
| User Groups | Other Resources | Press Releases | Company

© 1998-2024, Charles Arehart, SysteManage
Our Practice Makes You Perfect