Var Scoping - How and Where Do You Set Your Function Variables?

As everybody knows you should var scope ALL local function variables inside ColdFusion components, and these variables must be declared at the top of your function before any other code. If you place other code above a “var” scoped variable in your method signature an exception will be thrown.

Depending on how you define you methods this will probably be fine, but I got to thinking recently on how this works with exception handling within your function, specifically for try/catch blocks.

As you can’t add code above your var scoped variables there is no way to wrap your var scope block in a try/catch block. With this being the case you may have problems if an exception is thrown when you var scope a local variable.

An example of what I’m talking about:

1
2
3
4
5
6
7
8
9
<cffunction name="getWorkSchedule" access="public" returntype="query">  
  <cfset var oUser = getUserService().getUser() />

  <cftry>
      ...
      <cfcatch>
      </cfcatch>
  </cftry>
</cffunction>

In the fictional example shown above we have a method which returns a users work schedule, the first thing done is retrieving an instance of User from the UserService. As this has been set at the same time we var scope our variable (oUser), what happens if the call to getUserService().getUser() throws an exception? The exception would bubble up to the calling template (the template that called getWorkSchedule()) which might be fine but might also be unexpected.

This is just food for thought, it may be that instead you want to define your method like:

1
2
3
4
5
6
7
8
9
10
<cffunction name="getWorkSchedule" access="public" returntype="query">  
  <cfset var oUser = "" />

  <cftry>
      <cfset oUser = getUserService().getUser() />
      ...
      <cfcatch>
      </cfcatch>
  </cftry>
</cffunction>

Here we take the approach of always declaring our local variables as empty strings or integers etc (simple values) before setting explicit values into them. This means all your variables are cleanly defined and you can catch any possible exceptions if you need to inside the try/catch.

The only is that is feels like a bit of double handling because I need to my variables twice…

How do other people handle this? Lend me your thoughts :)

Comments