I have seen many beginners confused over the use of # sign in ColdFusion and most are so confused that they use it anywhere they can! I have been asked this question so many times, that I decided to write about it.
The basic thing you need to remember about using # in ColdFusion is that, “Does thing expect a string input”, “Do I need to use the value of a variable inside a string” Or, “Do I need to print a # in my string?”
“Does thing expect a string input”
Now, this is where most of the confusion is, the easiest way to identify is to use the intellisense/autocomplete feature of the IDE use, if the intellisense gives a quoted string beside the parameter, then it means the input is expected in a string format.
For Example.
In the above cfquery param all intellisenses would expect a quoted input in for datasource, name and all other parameters. Now, if you’re input is coming from another variable(In most cases a datasource saved in Application Scope or a Session Scope) then the right way to use it as datasource would be
Now let’s look at another case.
While looping over a query object using cfloop, the query paramater of the cfloop tag expects only the name of the queryObject variable, putting a pound sign, would not work here as # evaluates a variable and then uses it value. In some extremely rare cases where you have the name of the query variable store in another variable, you can use the pound.
Now, some developers to avoid the confusion, acquire a habit of using the # everywhere, making there code look like a twitter timeline. This has perfromance impact, as the # sign first evaluates the value of the variable and then uses it, using it everywhere would mean doing unneeded evaluations.
Example .
“Do I need to use the value of a variable inside a string”
Now this is the most appropriate use of # in coldfusion, it helps reduce the use of concat.
“Do I need to print a # in my string?”
This is another of the few uses of # in coldfusion, since # has a special meaning in coldfusion, sometimes it could be difficult where we have to use a # in an output. In that case we just use an extra # to escape the evaluation.
Example
I hope I have been, able to provide enough detail about the places where you should put a #, but if you feel I am incorrect, or you have more questions, please feel free to comment.