Rob,
This is a great question, and one to ponder for a while, no doubt.
Programmatically, there's nothing wrong with this approach. It works just
how you want it to.
Now let's think about what that's doing. Your CFC is just acting like a
collection in this case. This is basically how a map object works, a basic
collection in Java. put(), get(), size(), etc. If you're going to do that,
why not just use a collection object. There's a great one you can leech from
Model-Glue's CFCs, or you can literally use a java collection object (they
have a wide selection). However, think a little deeper - this is just a
coldfusion Struct. struct.key = value, #struct.key#, structCount(), etc. Why
don't you just have a public struct on your CFC? Well because that breaks
encapsulation.
Aha, encapsulation. If your CFC has a get() and set() function, and you
<cfset obj.set("ABC123xyz", obj)>, you (or another developer, and hopefully
not the end user), can put any kind of odd value in your CFC's local
storage. Even if you limit it to strings only, you can still be surprised by
an odd string (abc123xyz=nathan, who knows?). Explicit getters and setters
keep the peace here.
The only other thing I can think of is keeping with your convention. If you
have some objects with, say, exactly 3 fields always, or objects that do
something when you get or set a value, you'll want to use a getter/setter
like getName() or something. If your application is 50% getters & setters
and 50% get(key), set(key,value), you've lost your standard convention, and
you have to introspect (or view the source on) every CFC to make sure you're
doing it right.
Of course there arguments the other way. For instance a config object with
an unknown or ever-changing number of params it reads from a database or xml
file would not be as helpful to you if you had to constantly add and remove
getters and setters every time you needed to add and remove parameters from
your evolving application. For this case alone I think I would use the
universal set()/get() methods, or maybe just a plain old struct ;)
--
nathan strutz
http://www.dopefly.com/
On 6/14/07, Rob O'Brien <[EMAIL PROTECTED]> wrote:
>
> So, I'm just starting to go into OO-style CF development and I have a
> quick
> theory question... Mind you, this is brand new to me, so be gentle.
>
> When setting up my Location Bean, I started to go through each of the
> individual variables and created a get() and set() method for each of
> them.
> I got about halfway through and decided that I was tired of creating
> repetitive functions. I thought to myself, why can't I just create simple,
> dynamic get() and set() functions to handle all of these variables
> dynamically?
>
> Rather than setName(value), why not setVar("Name",value)?
>
> Since these methods are inherently dumb (nothing but a cfset and a
> cfreturn
> as I've seen implemented), why does it matter that I have an individual
> method for each? Validation would come in an additional business layer
> anyway.
>
> I guess I'm looking for pros/cons. This seems like a more dynamic, elegant
> way to set/get, but I'd like to know where the pitfalls are.
>
> Thanks!
> Rob
>
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Create Web Applications With ColdFusion MX7 & Flex 2.
Build powerful, scalable RIAs. Free Trial
http://www.adobe.com/products/coldfusion/flex2/?sdid=RVJS
Archive:
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:281181
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4