4

Say I have a very simple controller like so:

<cfcomponent extends="Controller">
    <cffunction name="hello">
        <cfset time = Now()>
    </cffunction>
</cfcomponent>

In straight ColdFusion / Railo, I would be locally scoping all variables within this...but every wheels example I see does not.

This is probably going to earn me the dumbest question of the year award, but it was something I was thinking about, since nobody ever seems to demonstrate their Wheels code scoped correctly?

I would write it as follows:

<cfcomponent extends="Controller">
    <cffunction name="hello">
        <cfset local.time = Now()>
    </cffunction>
</cfcomponent>

I am just not sure if Wheels perhaps does something to remedy this regardless, and that's why I see what I do everywhere...or is it just a case of bad programming?

Thanks! Mikey

Jeromy French
  • 11,812
  • 19
  • 76
  • 129
Michael Giovanni Pumo
  • 14,338
  • 18
  • 91
  • 140

2 Answers2

8

Yes, you should be scoping it.

In your first example, you are (by not scoping) in most cases setting variables.time, which is local to the component instance, not to the function - if this is intended as a function-local variable (i.e. local.time) but is in the component's variable scope, and that component is shared/persisted, this may cause issues (though perhaps ones that only reveal themselves under heavy load).

If putting the variable in the variables scope is deliberate, it should still be explicitly scoped (as variables.time) otherwise it may cause issues if used on a Railo server with the localmode setting enabled.

Due to a cfWheels design decision (see links in comments), putting variables in the variables scope is required to pass variables to the view, even though they may technically be local to the function/view. (The controller instance lives for a single request, avoiding the issues this normally entails.) As mentioned in the previous paragraph, the localmode setting (described below) means it is still recommended to explicitly scope when you are not in control of all servers the code will be deployed to.

Railo's localmode setting

Railo (since v1) has had an admin setting called "localmode" which defines whether assigning an unscoped variable will go to the local scope, rather than the component's variables scope - making explicit var/local scoping not required (if you know your code will only be run on Railo servers with that setting enabled).

Since that setting is off by default, and ColdFusion does not have such a setting, cross-engine-compatible code should always scope such variable assignments to avoid this being an issue.

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
  • Thanks! I just needed clarification really. Why on Earth people don't scope as a second nature to them is beyond me! Have a great day :) – Michael Giovanni Pumo Jan 28 '13 at 17:49
  • Check the cfwheels Google group for this question. Peter is correct but not for cfwheels and passing variables to the view. – Mike Henke Jan 28 '13 at 18:02
  • Mike, got a link? I don't see any appropriate subjects at https://groups.google.com/forum/?fromgroups#!forum/cfwheels – Peter Boughton Jan 28 '13 at 18:08
  • 1
    (If cfwheels requires it to be in variables scope for passing to view, it should still be scoped so that it doesn't fail if people are using Railo and have the mentioned setting enabled.) – Peter Boughton Jan 28 '13 at 18:09
  • I searched the group for var scoping and scoping, https://groups.google.com/forum/?fromgroups=#!searchin/cfwheels/var$20scoping/cfwheels/S3LhC1NQf8A/L37E0xXYAS0J https://groups.google.com/forum/?fromgroups=#!searchin/cfwheels/scoping/cfwheels/btVOVaKj0a8/AYpBcAAK2zYJ – Mike Henke Jan 28 '13 at 18:34
  • Noticed Peter said "deliberate" which is the case for cfwheels. As for the question, yes scope locally if you don't want the variable passed to the view, else scope into variables for clarity. – Mike Henke Jan 28 '13 at 18:40
  • Hrm, that _"there is no way to say method.getLocalScope()"_ remark (in linked discussion) is ... well, I wont go there now. :/ My point on always specifying either local or variables as appropriate stands - I've just updated the answer with a note on cfwheels/view variables. – Peter Boughton Jan 28 '13 at 18:53
4

It depends. If you want the variable to be shown in the view, scope it to variables. If you want the variable to be only in the controller, scope it to local.

Mike Henke
  • 864
  • 1
  • 8
  • 22
  • This is the correct answer. Generally, keep any variables that need to available in the view in the default `variables` scope. – Chris Peters Jan 29 '13 at 14:28