CFML Beginners Must Read

Word Count: 500

I have been digging through a bunch of old code for review and I would like to share some findings with you. If you are a beginner or fairly new to the CFML world this will be some good information to know. One of the very first things I notice when doing code reviews is the misuse  of pound signs.

When a CMFL server encounters pound signs inside of a cfoutput tag body it will check to see if the text is either a variable or a function. If it is either it will replace the text with the result of a the variable or function. In this example we see a variable msg being set and then evaluated in the cfoutput tag body. The result on screen is Hello World.

If you try and output the variable msg and it has not yet been set you will get an error.

The real reason for this article comes into play when you are trying to evaluate a variable is some logic. Let's say I am working on an e-commerce application and I want to check the URL to see if the id of a product is a numeric Id greater than zero. I think we have all done something similar to this before.

What if you wanted wanted to check the users session to find out if the user was purchasing from USA or Canada. Again this is nothing new we could just do something like this.

The problem comes in time and time again that new CFML programmers want to wrap pound signs around these variables when using them in logical conditions. The main problem I always see is usually in some cfif statement. Taking our 2 examples from before this is usually what I will come across.

This is one of the first signs to me that you are a novice CFML programmer. This is not a bad thing so don't think I am trying to belittle you or anything it is just something you learn with experience. My plan with this article is to get you ahead of the curve a little quicker.

Comments

#1 Posted By: Peter Boughton Posted On: 10/13/08 1:49 PM
That should be an AND not OR in your 3rd and 5th examples?
#2 Posted By: Dan Vega Posted On: 10/13/08 1:53 PM |
Author Comment
Nice catch, I just threw the code together so thank you.
#3 Posted By: andy matthews Posted On: 10/13/08 8:59 PM
I wouldn't say "novice programmer", but rather "novice CF developer". Even programmers experienced in other languages can make the mistake of the pound signs being used incorrectly.

Also, one thing to remember is that I believe older versions of the WACK actually show code examples with # inside if statements.
#4 Posted By: Dan Vega Posted On: 10/13/08 9:09 PM |
Author Comment
Good call, I updated it.
#5 Posted By: Ben Nadel Posted On: 10/14/08 10:07 AM
AMEN! :)
#6 Posted By: johan Posted On: 10/14/08 6:19 PM
All good but there is an exception with dynamically named variables which looks "wrong" but is correct, for example:

<cfset i = "myVar"/>
<cfset "#i#" = structNew()/>
<cfset "#i#.myKey" = "Hello World"/>
<cfdump var="#evaluate(i)#"/>

Dump will show myKey = Hello World
#7 Posted By: JD Posted On: 10/15/08 11:40 AM
Johan:

I always put this into a scope like:

<cfset i = "myVar"/>
<cfset variables["#i#"] = structNew()/>
<cfset variables["#i#.myKey"] = "Hello World"/>
<cfdump var="#variables["#i#"]#"/>

Makes it more readable for me and you don't have to evaluate anything.
#8 Posted By: Jason Fisher Posted On: 10/15/08 12:23 PM
JD:

To Dan's point, further simplify with even fewer # signs:

<cfset i = "myVar"/>
<cfset variables[i] = structNew()/>
<cfset variables.i["myKey"] = "Hello World"/>
<cfdump var="#variables.i#"/>
#9 Posted By: Dan Vega Posted On: 10/15/08 12:29 PM |
Author Comment
Great point Jason, I was waiting for someone to point it out!


Post Your Comment

Leave this field empty







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.