By default are <cfarguments ...> protected from outside intervention?

Adam Wayne Lehman
Web Systems Developer
Johns Hopkins Bloomberg School of Public Health
Distance Education Division


-----Original Message-----
From: Sean A Corfield [mailto:[EMAIL PROTECTED]] 
Sent: Friday, January 24, 2003 2:07 PM
To: CF-Talk
Subject: Re: Protecting scope of <cffunction> variables

On Friday, Jan 24, 2003, at 06:56 US/Pacific, Jon Gunnip wrote:
> I am writing UDF's on MX using <cffunction>, and I would like to know 
> what
> the best way to create local variables for use inside my functions 
> that will
> not interfere with variables in the caller's scope.

As others have said - use "var". One practice that many people seem to 
like (and about which I'm still on the fence) is to declare a "local" 
struct and use that throughout:

> <cffunction name="add_two_numbers">
>   <cfargument name="first" required="true">
>   <cfargument name="second" required="true">
        <cfset var local = structNew()>
>   <cfset local.sum=arguments.first + arguments.second>
>   <cfreturn local.sum>
> </cffunction>

This has the nice side effect that you only need one "var" declaration 
at the top of your function and all subsequent local.xxx references are 
safe from "outside influence".

It has the downside that you end up doing a struct dereference for 
every local variable.

Note that if you had your original code inside a CFC, the unadorned 
"sum" variable would become a private (OK, 'protected') instance 
variable inside the component instance and not accessible from outside. 
See below:

> <!--- page that uses the function --->
> <cfinclude template="functions.cfm">

Becomes:
        <cfset adder = createObject("component","adder")>

> <!--- using sum to track addition of numbers --->
> <cfset sum=12>
>
> <!--- sum is now 12 --->
> <cfset result=add_two_numbers(1, 2)>

Becomes:
        <cfset result = adder.add_two_numbers(1, 2)>
        
> <!--- sum is now 3 (changed in function), when I want it to be 12 --->

No longer true - unadorned "sum" is protected inside CFC.

> <cfset sum=sum + result>
> <!--- sum is now 6, when I want it to be 15 --->

sum would be 15.

Sean A Corfield -- Director, Architecture
Web Technology Group -- Macromedia, Inc.
tel: (415) 252-2287 -- cell: (415) 717-8473
aim/iChat: seancorfield -- http://www.macromedia.com
An Architect's View -- http://www.macromedia.com/go/arch_blog

ColdFusion MX and JRun 4 now available for Mac OS X!
http://www.macromedia.com/go/cfmxosx


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm

                                Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
                                

Reply via email to