[thelist] CF -- have a Q need a Tip to clean-up my webs.

Rory.Plaire at wahchang.com Rory.Plaire at wahchang.com
Fri Oct 12 19:45:06 CDT 2001

You know,

One really feels like one has attained a new level of development when one
can look back on the code one has written or the design one has conceived
and say: "Yuk!"

  "Dissatisfaction with oneself is a sign of progress."
  --Diary of Mírzá Ahmad Sohrab, 1914, 
    quoting a talk of 'Abdu'l-Bahá

First, the "Q:"

I've done my reading. Oh yea, I've done it.

But, I can't find why I need to cflock my access to the query when I do

<cflock timeout="5" scope="session" type="readonly">
	<cfset myqry = session.myqry>
<!-- this loop doesn't work without locking! -->
<cfloop query=myqry>  

I suspect I know... reference vs. value assignment in cfset.

But, I need to _know_.

So, here is a nice tip or two I've found in the process of understanding how
wrong I've been and non-optimal my CF code was.

<tip type="ColdFusion Optimization" author="rory">
If you are making a UDF (User Defined Function or Custom Function) and need
to employ it in a number of your templates, you read to documentation to
discover that you should put it in the Application.cfm tag.

However, this causes multithreading issues, and to use it, you should,
really and honestly, cflock the bugger.

To avoid this bit of tedium and performance hit, try putting the UDFs in a
"library file" and cfincluding that in the Application.cfm file. Since the
server stays in a multi-threaded condition, the performance is increased
and, more importantly, cflocks no longer grow like the pestilent weed in
your carefully cultivated code.

<tip type="ColdFusion Optimization" author="rory">
UDFs (User Defined Functions [a.k.a. Custom Functions]) can save a lot of
typing. Even more so when you manage your data in structured form. For
instance, you could throw around your variables one-by-one, like so:

<cfloop query="qry">
<!-- now for my UDF... -->
<cfset myvar = qryRowToStr(qry.Col1, qry.Col2, qry.Col3)>

This is great if you are Mavis Beacon or some other all-star typist.

However, I am a poor typist (captive in a culture which cannot embrace
Dvorak or Dragon).

So, you could pass the whole query to the function instead:

<cfloop query="qry">
<cfset myvar = qryRowToStr(qry)>

In the function, you can access the query like so:

function qryRowToStr(qry)
{	var iRow = qry.currentRow; //get the current row of the query
	var str = "";

	str = qry["Col1"][iRow] & " " & qry["Col2"][iRow]

	var arrCols = ListToArray(qry.columnList);
	for(var colNum = 1;i <= ArrayLen(arrCols); i=i+1)
		str = str & qry[arrCols[colNum]][iRow];


You could even iterate over an entire query, if you wanted!

<rory disposition="asleep at the wheel" alt="8)"/>

More information about the thelist mailing list