I've been experimenting with a similar technique recently. By and large 
it's quite effective, and it's easy to spot places you've missed the 
local prefix because ColdFusion stops with a bang. But unfortunately it 
doesn't FORCE you to declare all variables as members of the local 
scope, so if you define a variable without the prefix then you can 
continue to refer to it without the scope qualifier and the code will 
still run. It's reasonably easy to spot incorrect <cfset> statements 
because they should all look like <cfset local.whatever...>, so a quick 
eyeball of the code will usually throw out those errors; BUT it is all 
too easy to forget to scope implied variable instantiations e.g. 
<cfquery name="myQuery"> should actually be written <cfquery 
name="local.myQuery">.

So it's a lot better than nothing - but still not perfect, so hopefully 
somebody has a more robust solution...

-- 
Andy



Ian Buzer wrote:
> Hi everyone,
>
> I find myself getting more and more wound up that variables set within a cfc 
> function are not scoped to that function but to the entire cfc.
>
> I'm ending up with a whole series of <cfset var myVariable = ""> at the 
> beginning of the function which is messy and to me is asking for trouble, 
> especially if myVariable is not a string. It's also very easy to miss one.
>
> So I've had an idea. At the beginning of each function, I'm going to add:
>
> <cfset var local = StructNew()>
>
> Which effectively creates me my own local variable scope. I can then create 
> variables like local.myVariable, safe in the knowledge that they are local 
> and thread safe.
>
> Good idea? Bad idea?
>
> Thanks
> Ian
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:235868
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to