When and when not to use hash # symbol in ColdFusion?
Asked Answered
C

3

5

I use the # symbol around every dynamic value in my application and after posting some of my code on here for help, I've been told there's no need to use the # in many places e.g. <cfif> statements.

So I have started removing the # symbols, until I realised I broke my application because I removed the # symbols from the value="" attribute of a <cfprocparam> tag.

I am confused as to:

  1. Why use the # symbol is some places and not others (and what is the benefit of not using it?)
  2. Why if they are not required in <cfif> and <cfargument> tags are they suddenly required in <cfprocparam> tags?
  3. Due to this lack of consistency, is it not better to just wrap hashes around every dynamic value in the first place?
Condemnatory answered 4/2, 2014 at 21:48 Comment(4)
Not that this is not a valid question, but .. did you do a search before posting? Because this question comes up a lot. For example a five second search turned up: coldfusionmuse.com/index.cfm/2011/2/10/… and help.adobe.com/en_US/ColdFusion/9.0/Developing/…Logography
It's with no small measure of irony that I note had you simply typed your question title into Google instead of Stack Overflow the answer would have been the first link (it's now second, after this question). And the second link. And the fourth link. Etc.Mcauliffe
Part of the reason for posting it as a question was that I wasn't sure of the performance benefits of not using the hash symbols if they are unnecessary.Condemnatory
If you review the links above, the second one addresses that very question. Supposedly it does have an impact, though generally it is considered negligible.Logography
M
6

There is no inconsistency (or very little: none of what you cite are inconsistencies), it's just you not understanding the rules (which are pretty basic really). It's all in the docs: "Using number signs"

In short, within a CFML statement, all elements are considered CFML, so there is no need to specifically mark them as such. EG:

<cfset myVar = someFunction(anArgument)>

There is no ambiguity there that myVar, someFunction and anArgument are anything other than CFML constructs, so no need to do this sort of thing:

<cfset myVar = #someFunction(anArgument)#>

As some people are inclined to do.

In the middle of text or within a string, there is ambiguity as to what's text and what's CFML, so one needs to use pound signs to mark them as such. EG:

<cfset myVar = "The time is #now()#">

It's necessary to us pound-signs there to disambiguate now() as being a CFML statement, an not just part of the string, eg:

<cfset myVar = "CFML has a function now() which returns the current timestamp">

Equally:

<cfquery>
    SELECT col1
    FROM table2
    WHERE col2 = #someValue#
</cfquery>

There would be no way of knowing someValue is a variable there without marking it as such.

That's basically it. It's not complicated.

Mcauliffe answered 4/2, 2014 at 22:15 Comment(6)
how come <cfdump var=""> needs the pound symbols when there is no ambiguity that it is a variable?Condemnatory
Because you can do this `<cfdump var="moo" /> to dump out the string 'moo'.Peony
Same as above. <cfdump var="foo"> will dump the string foo. <cfdump var="#foo#"> will dump the contents of the variable foo. The correct question here would be why <cfdump var=foo> dumps out the string "foo" not the variable foo. The answer - as far as I can tell - that CFML sux a bit. That's the only ambiguity I know of in CFML in this context (that's not to say there aren't others; I just don't know them off the top of my head).Mcauliffe
The value of var doesn't need to be a variable reference, btw. It can be any CFML expression, or as mentioned above, a literal value. So there is ambiguity.Mcauliffe
For sake of thoroughness, I would add if you want an actual hash tag character as part of your string/output, you would use ##.Shadow
<cfparam name="name" default="#ArrayNew(1)#"> (or <cfparam name="name" default=#ArrayNew(1)#>) seems to be another one needing pound-signs, when needs to be populated with a complex data type.Mass
P
2

Rule 1: If you are inside of quotes, then you are pushing a string. If you want a substitution, the you use #name#

Rule 2: If you are inside of a <cfoutput>, you are generating a string.

While it is possible to write

<cfif "#name#" EQ "bob">Hi Bob</cfif>

It is easier to write

<cfif name EQ "bob">Hi Bob</cfif>

Rule 3: I think that <cfoutput query="qryData"> is kinda wrong, I have written it so much, I don't think much of it.

Perfective answered 4/2, 2014 at 22:12 Comment(2)
In rule 3, you are specifying the name of the query variable (so it's a string) not the value of the recordset itself. So it's consistent as far as using pound signs go, but the syntax of <cfoutput> itself is a bit daft.Mcauliffe
Your reference to <cfoutput> is slightly confusing, as it's not just <cfoutput> that works like that (<cfquery> being the most obvious other one).Mcauliffe
F
1

The # symbol is required around a variable only when you need to evaluate the contents of that variable. For example, when you need to out put that variable in a view.

You don't need them in cfset or cfif because the content of the variables is used in the Set or comparison.

You shouldn't be using the value of variables in the cfargument tag. You might however pass in a variable to as an argument without first evaluating it eg. myFunction(myarg=myVariable)

Cfprocparam you need to pass the value. You may be confusing how you're passing the variable and the value.

Value="myVar" would pass "myVar" as the value, where as value="#myVar#" would evaluate myVar and pass its content to value. value=myVar would pass myVar to value.

No real inconsistencies in the examples you give. That's not to say that there aren't a few inconsistencies kicking around in ColdFusion. ;)

Don't be hashing everything. It's messy and means that you add an evaluation step in everything bit of code you write.

Franchescafranchise answered 4/2, 2014 at 22:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.