Jim Priest
01/20/2023, 8:59 PMAdam Cameron
var foo and thereafter don't scope it.
Your method code should not be so long that you lose track of whether a variable is correctly `var`ed or not. If it is... that's your problem. Not local.foo or var foo.richard.herbert
01/20/2023, 11:12 PMlocal.foo when you have to declare a variable in a tag or function like...
<cfquery name="local.myQuery" ...>
...but then reference it thereafter without the scope, like...
<cfloop query="myQuery" ...>Viral
01/21/2023, 6:30 AMstructKeyExists .
StructKeyExists(local,"foo")
It doesn't work with var scope so you will need to use isdefinedzackster
01/21/2023, 10:10 AMAdam Cameron
var is a keyword that is used in a variable initialisation statement that puts the variable in the local scope. The scope in all cases discussed here is the local scope.
Once a variable in the local scope is initialised, one can safely reference it without the scope: the scope with be inferred during code execution. Whilst CFML engines do that scope look-up thing, the local scope is the first one checked, so there is absolutely no harm in not being explicit about the scope, plus it cuts down in pointless boilerplate code, and makes the code easier to read.
This is, of course, predicated on the quality, length and design of the function in question. If it's a haphazard 500-line mess of spaghetti that ignores good coding practices (such as the SOLID principles), then yeah you might need to explicitly scope stuff. But - again - that's down to the code being poorly implemented. Not about scoping per se.
And obvs(?) if one is maintaining legacy code there is an increased likelihood of it fitting the description of "haphazard 500-line mess of spaghetti that ignores good coding practices (such as the SOLID principles)", so... yeah... act accordingly. I did not mean that as an indictment of anyone writing new code today; it's an indictment of the code they're writing into.zackster
01/21/2023, 11:21 AMAdam Cameron
Adam Cameron
richard.herbert
01/21/2023, 11:25 AMAdam Cameron
zackster
01/21/2023, 11:53 AMAdam Cameron
zackster
01/21/2023, 11:56 AMAdam Cameron
Adam Cameron
zackster
01/21/2023, 11:59 AM