macromedia.coldfusion.advanced_techniques
[Top] [All Lists]

Re: CFC best practices

Subject: Re: CFC best practices
From: Adam Cameron
Date: Sat, 28 Feb 2009 17:05:57 +0000
Newsgroups: macromedia.coldfusion.advanced_techniques

> Hello. Please help me out with a little issue I have. I've recently started 
> working with CFC's and there is something that I want to get clear. I have 
> seen 
> two different ways to write CFC's (although I'm sure that there are many 
> others). Let me get to the point. The first method uses an init() function 
> that 
> acts as a constructor.

Good question!


>      <cffunction name="init">
>               <cfscript>
>                       variables.attributes = structNew();

One thing you could do with your init() method is to have optional
arguments for each of your object properties here too.  This allows the
calling code to init() the object and set some properties in one hit,
rather than having to init() then set().


>                       variables.attributes.cityId = 0;
>          </cfscript>
>           <cfreturn this>
>      </cffunction>

Here's a question for you.  I see a lot of people doing this, and I just
don't get it.  You've got this <cfscript> block, but you stick the return
statement outside it.  Why not do this:

                        variables.attributes.cityId = 0;
                        return this;
          </cfscript>
      </cffunction>

It's a very trivial thing, but it piques my interest.


>      <cffunction access="public" name="getId" returntype="numeric">
>       <cfreturn This.id>
>      </cffunction>
>  </cfcomponent>

I would say with properties exposed in the THIS scope, one would not
necessarily bother with getters.  The whole point of THIS-scoped variables
is that they're accessible directly.  Using your example code, one would
just do personDTO.id in the calling code.

That said, I would almost never store my object's properties in the THIS
scope, I'd keep them safely locked away in the VARIABLES scope.

-- 
Adam

<Prev in Thread] Current Thread [Next in Thread>