Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
like image 516
volume one Avatar asked Jan 11 '23 06:01

volume one


2 Answers

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.

like image 106
Adam Cameron Avatar answered Jan 22 '23 04:01

Adam Cameron


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.

like image 23
James A Mohler Avatar answered Jan 22 '23 04:01

James A Mohler