House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community


ColdFusion Articles


I just answered a question for someone on the Macromedia forums and it reminded me of an issue of style that I've wanted to write about. The poster wanted a 6 digit random number. Easy to do using 6 RandRange() functions in a row. I posted it this way:

<CFSET iSixDigit = RandRange(0,9)& RandRange(0,9)& RandRange(0,9)& RandRange(0,9)& RandRange(0,9)& RandRange(0,9)>

Another poster did it this way:

<cfset no = "">
<cfloop index="i" from="1" to="6">
<cfset no = no & RandRange(0,9)>
</cfloop>

Now, obviously they both work but there's a big difference between them. The second version looks more elegant than mine. If you were looking at the two solutions, you'd go with the second because it just looks like it's better (and if it was for a job, hire the writer of the second).

So why did I, a rather high level ColdFusion programmer do it my way? Because it saves on processing operations. In my case, I've got 1 CFSET operation and 6 function calls. In the loop case, there's 7 CFSET operations, 6 function calls as well as a CFLOOP. That's a few more operations than what's really needed (IMO).
I've seen a lot of these type things over the years and I've always felt that they were wasteful. Yes, I know it's probably less than a fraction of a millisecond, but it's still processing time.

Update: A new, cleaner and more efficient solution was just posted to the thread.

<CFSET iSixDigit = NumberFormat(RandRange(0,999999), '000000')>

This is a total of 1 CFSET and 2 function calls. Very nice.


There are a few places in ColdFusion where a time value is needed in a certain format. The best know are the CachedWithin parameter in CFQUERY and the timeout parameters in CFAPPLICATION. All documentation for these say to use the CreateTimeSpan() function to create a time value. Now this seems a bit useless. The time value created (actually, just a number value) is not dynamic. It's being created every time just because some document said so. Want to save an operation. Just replace the CreateTimeSpan() with a number instead.
Here's how it works. Running CreateTimeSpan() with a value of 1 day results in a return of 1. This basically means that if you want your CachedWithin value to be 1 day, you just put in the number 1 rather than the (useless) function. But what about hald a day? Just use logic and its simple. 1 is 1 day so .5 is half a day (12 hours). 6 hours is .25 and you just keep going from there. The only problem is that as you get smaller and smaller in time, the resultant numbers get longer and longer.
But do we care about longer numbers? No. We care about using 1 less function for each cached query. We care about 1 (or more) less function for our CFAPPLICATION tags. We care about going totally overboard on our optimizations. And why not? It doesn't hurt anyone and telling people why gets you points for knowing some deep secret about how CF works.
Just as a side note, as you get to smaller values, you'll start to get some irrational numbers. Just round up to the 3rd or 4th decimal place and don't sweat it. Using .0208333333333 for a 30 minute time value is nice, but .02084 will work just as well. And wy round up? Because its better to give a little extra time than to give less than expected.
24 hours - 1
12 hour - 0.5
6 hour - 0.25
3 hour - 0.125
1.5 hour - 0.0625
60 minutes - 0.0417
45 minute - 0.03125
30 minute - 0.02084
15 minute - 0.010417
10 minute - 0.00695
5 minute - 0.003473
1 minute - 0.000695

Methodology - An organized way or writing and placing your code. Some methodologies include a framework, but that is NOT needed to be a methodology. There is NO standard methodology, just a number of well known ones. Fusebox 1 was a pure methodology. Mach-II is not. EVERY programmer should have a set methodology that they use for their coding practices.
Framework - A collection of code that is used as a template for writing applications. A framework allows a single, 'standard' code base to be used for all parts of the application and allows others to work on the application faster because they are using a 'standard' core of templates. A framework is used for speed of development and standardization among programmers. By its very nature, any framework will have extra code that is not optimized to a site. Mach-II is a framework.

Pods are a concept that was released with the Central product by Macromedia. From an article on Breeze, it seems that the concept has grown past Central into some form of standard for Flash programming. A working definition of a pod might be:
"A pod is a SWF which contains the UI, Code and initial data for an application. This application is not meant to be independent but instead is meant to be a sub-component of a larger application.
In layout terms, a webpage that has a standard header, sidebar, body and footer could be said to contain 4 pods, 1 for each section. Alternately, it can be said that each section displayed in the body section is a pod and the surrounding layout is the containing site/application."

This is a 'feature' that seems to have existed since CFMX 6.0. Basically, if you try to access a query element greater than the number of elements in a query, the result will be a blank.
Try it yourself by running a query with a single return row. Then try to access the 100th element of the query
Queryname.Columnname[100]
Logically, there should be an error for trying to access an item outside the data range. In CFMX queries, this does not happen.
Read more on this CF-Talk thread

Some people create their own multi-part emails using CFMAILPARAM tags to create mime parts. This works great in CFMAIL pre-6.1 but in 6.1 this fails. You have to use the new CFMAILPART to send multi-part mail as of 6.1

When moving from CFMX to CFMX 6.1, any special class path added in the admin will be rewritten with the slashes removed. This will cause an error somewhere down the line.

in CF 5, when a RegEx pattern was run and a subexpression was set to optional (i.e. had a question mark after the subexpression) AND the option to return all subexpressions was made within the REFind() tag, the optional subexpression was not added to the array if it returned nothing. Let me illistrate with some code:
^([-a-z0-9_.]+@[^[:space:]]+) *\(?([^)]+)?\)?

This says:
^ - start at the beginning of a string
( - Start a subexpression and return everything in it as one item in an array created by the REFind() tag
[-a-z0-9_.]+ - match one or more of these characters
@ - match an @
[^[:space:]]+ - match one or more of anything that is not a space type character
) - All of the above is one subexpression
match 0 or more spaces (the actual space character)
\(? - match the literal character ( that may or may not exist
( - Start a subexpression and return everything in it as one item in an array created by the REFind() tag
[^)]+ - Match anything that is not a closing parenthesis
)? - This subexpression may or may not exist
\)?- match the literal character ) that may or may not exist
The second subexpression is optional. In CF5, there is no place in the returned array for it if it does not exist. In CFMX, an entry in the array always exists for it, even if the entry is 0.

This is an interesting error that comes up once in a blue moon and probably has to do with a failure somewhere in the install. Basically, once you install and are about to go to the migration tool, you find that you just can't log into the admin to finish your migration. The reason is that the ODBC services are not running. You try to start them up and they fail. Why? Bacause there's an error in their config files.
The fix is rather easy. Do a search on the string "c:\program files\merant" in the c:\cfusionmx\db directory (or whatever drive you've installed it on). You'll find 3 files. Replace that string (c:\program files\merant) with the string "c:\cfusionmx\db" (or whatever drive letter you have the cfusionmx directory in). After replacing all of the instances in all 3 files, start the services and finish migrating. Easy to do and only 3 people have ever reported seeing it.

Along with the ODBC error above, someone was getting 500 errors on IIS from every attempt to access a CF page, including the admin (needed for the final migration checks). The answer was to rerun the IIS connector for CFMX. Fixed the problem right up. As I don't use IIS (I'm a Website user since day 1), I can't go into more details.

In previous versions of CFMX and/or Website, any attempt to use the ancient practice of Search Engine Safe (SES) Urls resulted in failure. I don't know if it was the latest Website or CFMX 6.1 but I can use them to my hearts desire now. I've even went to far as to code the logic into a CFC, cache the CFC into the server scope and call it at will. Fast and effective.


Designer, Developer and mobile workflow conference