ColdFusion is very easy to start learning. As a tag-based language, it is very approachable to anyone who is comfortable with HTML. One area where many CF novices have trouble, though, is with improper use of the pound sign (#), that little mark that tells the ColdFusion server to return the value of a variable. Novice CFers often overuse the pound sign, surrounding a variable name when it is not needed or even desirable.

I believe proper use of the pound sign can be distilled down to two basic rules. Pound signs are needed only in the following circumstances:

  • If the variable name is surrounded in quotes
  • If you want to write the value of the variable to the screen/document/email
The first rule is where many CF novices trip up. When working with a variable name inside a tag, pound signs are often not needed. Here are a few examples, with corrections, of improper pound sign usage (I have seen all of these when conducting code reviews):

view plain print about
2<cfset #myvar# = 1>
5<cfset myvar = 1>
8<cfset myvar = #listLen(mylist)#>
9<cfset myvar = #listLen(#mylist#)#>
12<cfset myvar = listLen(mylist)>