What does:
        <cfset instance = structNew()>

do for your component?

Andy

-----Original Message-----
From: Sean A Corfield [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 30, 2003 11:19 AM
To: CF-Talk
Subject: Re: CFCs - property access best practices


On Thursday, Jan 30, 2003, at 07:17 US/Pacific, webguy wrote:
> Look at Seans excellent rules ..
> http://www.corfield.org/coldfusion/codingStandards.htm

Thanx. Yes, I highly recommend you use getter/setter methods. Ray gave
a good reason but wasn't specific. Here's some more detail:

It may be a simple property today but in the future it might be
calculated or it might be refactored into another CFC: using getter /
setter methods means your changes will be localized (you only have to
change the CFC, not all the code that uses it).

It may be a simple property today but in the future, you might want to
apply validation when you set it (e.g., an age should be a positive
integer). You can add validation logic to the setter - without changing
code that uses the CFC.

Furthermore, you want to use the unnamed scope for your instance data
(and this seems to be a popular idiom):

        <!--- person.cfc --->
        <cfcomponent>
                <!--- create a single 'handle' for your instance data: --->
                <cfset instance = structNew()>
                <!--- setter: --->
                <cffunction name="setFirstName" returntype="person">
                        <cfargument name="firstName" type="string" required="true">
                        <cfset instance.firstName = arguments.firstName>
                        <cfreturn this>
                </cffunction>
                <!--- getter: --->
                <cffunction name="getFirstName" returntype="string">
                        <cfreturn instance.firstName>
                </cffunction>
        </cfcomponent>

Some people have their setter functions return the object (as above),
others prefer to return nothing - like this:

                <cffunction name="setFirstName" returntype="void">
                        <cfargument name="firstName" type="string" required="true">
                        <cfset instance.firstName = arguments.firstName>
                </cffunction>

I prefer the former since you can then "chain" method calls in cfscript:

        <cfscript>
                me = createObject("component","person");
                me.setFirstName("Sean").setLastName("Corfield");
        </cfscript>

With the latter style of function definition, you have to say:

        <cfscript>
                me = createObject("component","person");
                me.setFirstName("Sean");
                me.setLastName("Corfield");
        </cfscript>

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
Signup for the Fusion Authority news alert and keep up with the latest news in 
ColdFusion and related topics. http://www.fusionauthority.com/signup.cfm

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

Reply via email to