Best practice to explicitly scope a cffunction defined in cfm

317 Views Asked by At

I understand that in ColdFusion, explicit scoping is a Good Thing. Since learning this, I try to explicitly scope all variables, even for variables like queries, for example, <cfquery name="local.myQuery"> or <cfquery name="variables.myQuery">.

My question is how to do explicit scoping when defining a function inside a .CFM page, which will be used only on the same page. Can I do <cffunction name="variables.myFunction"> or something similar?

3

There are 3 best solutions below

2
On

Posting my own answer with what I've found so far. The link from @Ageax's comment led me to this ColdFusion documentation on Specifying the scope of a function. It says:

When you define a UDF, ColdFusion puts it in the Variables scope.

You can assign a UDF to a scope the same way you assign a variable to a scope, by assigning the function to a name in the new scope. For example, the following line assigns the MyFunc UDF to the Request scope:

1: <cfset Request.MyFunc = Variables.MyFunc>

The way I understand this, it means that I should simply use <cffunction name="myFunction"> to define the function, but I should use explicit scoping when calling it, like variables.myFunction().

2
On

The best practice would be to take that function out of the cfm page and put it into a cfc class, then create an instance of the class from your page and use it.

Everything else will eventually lead to tears. Coldfusion will throw an error if you try to define a function with the same name twice (so if you ever replicate this function in some other cfm that includes or is included by this page, it will crash).

Roughly speaking, this should be your Thing.cfc file:

<cfcomponent>
  <cffunction name="myFunction">
  </cffunction>
</cfcomponent>

and this should be your cfm:

<cfset thing = new Thing()>
<cfset thing.myFunction()>

myFunction will be scoped only to the Thing class, and you will be able to define a function of the same name in other classes, or even override it in descendant classes.

0
On

Despite the fact that functions in ColdFusion are able to live in different scopes (default is variables) you should not change that or play with it. Just use components to apply encapsulation or leave then in the variables scope. If you try to define a function with the same name (in the same .cfm or in included ones), you will get an error and that should be fine, so you can't overwrite a function like that.

You should scope the variables inside a function. You don't do that scoping the name of the function but scoping the variables inside then. Good practice would be using the local scope or the var prefix.

<cffunction name="foo">
 <cfset var a = 123>
  <cfset local.b = 345>
</cffunction>

In Lucee you can define globally the whole scope of a function using localmode="modern"

function foo()localmode="modern"{...}