As others have pointed out, getters and setters are there for encapsulation.
For example, if you have a getPrice() method on a Product, right now it
might return just a price instance variable. But later you may need to add
in much more complex logic such as tiered pricing for certain customers,
sales, etc. If you weren't using a getter but were instead directly
accessing the "price" instance variable, you'd have problems.

That said, in general getters and setters are evil and I feel they are
overused in may cases. They promote a more procedural programming approach
and fly in the face of good OOP. One should normally not be asking an object
for data, but should be telling the object to DO something based on its
data. Getters and setters do offer encapsulation, but they are actually only
slightly less brittle than direct variable references. The more one
interrogates an object to get at its data, but more one drifts towards a
procedural approach. OO is about telling objects to do something with the
data they contain, not asking them for their data. This is the root of the
saying "Tell, don't ask" in OOP.

In practice about the only time I use a getter is if I have an object and I
am actually outputting its data to the view. And even that is a potentially
strong argument for allowing objects to render themselves instead (through a
composed rendering object of course, the object itself shoudn't know or care
about how it may eventually be presented). I'm still on the fence in that
regard but the idea has a number of advantages. That might be a subject for
another thread though.

Regards,

Brian


On Mon, Jun 23, 2008 at 12:46 PM, Will Tomlinson <[EMAIL PROTECTED]>
wrote:

> I'm just not understanding why I'd need a getter and a setter in my cfc.
>
> My app works just fine without setting anything in the variables scope of
> my cfcs. You submit a form, it adds the values to my db. You run a query and
> get all the values back out.
>
> What is the value of keeping each value in its own variables scoped
> variable? Where does it come in handy? Where are you supposed to use it?
>
> Here's a few lines from the legacy samples.
>
> <cffunction name="setLastName" access="public" output="false">
>        <cfargument name="lastName" type="string" required="true" />
>        <cfset variables.lastName = arguments.lastName />
> </cffunction>
> <cffunction name="getLastName" access="public" returntype="string"
> output="false">
>  <cfreturn variables.lastName />
> </cffunction>
>
> Guess I'm missing something since I don't have an OO background. Googled a
> little but still confused.
>
> Thanks,
> Will
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to 
date
Get the Free Trial
http://ad.doubleclick.net/clk;203748912;27390454;j

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:307979
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to