After reading your reply, I wanted to start the first rewrite with this: <cfcomponent output="false"> <cfset init()>
<cffunction name="init" access="public" returntype="void" output="false"> <cfset Variables.Counter = 0> </cffunction> But then it occurred to me that you want me to write even this simple little assignment statement as another object. Wow! I've got to think about this... -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Barney Boisvert Sent: Friday, October 14, 2005 6:29 PM To: [email protected] Subject: Re: [CFCDev] Inside the CFC: use Getter/Setter or Instance Scope On 10/14/05, Phillip Senn <[EMAIL PROTECTED]> wrote: > Here's what I've learned so far: > 1. > <cfset Variables.Counter... in CF is like > DIM Counter as Variant in VB, declaring a local variable. > <cfset This.Counter... in CF is like > PUBLIC Counter as Variant in VB, declaring a global variable. I don't know VB, but that sounds right. 'this' scope is available inside and outside a CFC instance, 'variables' scope is only available inside. > 2. > Putting all your variables into a structure is a good idea for housekeeping > purposes, but to call your structure "instance" is intellectually dishonest. > I don't believe you are instantiating an object, and calling your structure > "instance" tells me that you'd like for it to be an object, but really > you're just mimicking OOP. You can absolutely instantiate a CFC, and it becomes an instance of that CFC. But calling it 'instance' is just convention. I happen to call mine 'my' instead. As long as you're consistent it doesn't really matter, but using conventions can make porting code and/or developers easier. The point is that CF's nested-struct nomeclature allows a very easy way to differentiate between instance state and transient data it might also need to store. In Java (and probably most other languages), you have to use naming conventions, but with CF's structs, you can package the variables together, and then act on the separately, which is very useful in certain situations. > Admittedly, I don't understand OOP so I shouldn't be criticizing, but > calling a variable "Instance" confused me, even though I knew it was only an > example. > > OK, now that I've offended some people, let me show you my humble offering > and let you tear it to shreds like Ray Camden would do if I were to submit a > "guess the number" page: > > The file "mycomponent.cfc" contains: > <cfcomponent> > <cfset Variables.Counter = 0> Is this instance state, or is it some transient instance variable? Named like this, it should be the latter. And why isn't it in the (undeclared) init method? It ought to be. > <cffunction name="getAge" access="public" returntype="string" > displayname="Get a person's age" hint="Plugh"> > <cfargument name="BirthDate" type="date" required="yes"> > <cfargument name="CurrentDate" type="date" default="#NOW()#"> > <cfset Variables.Counter = Variables.Counter + 1> Uh-oh, you're using your transient state in a way that makes me thing that it's not really transient. You sure this shouldn't be variables.instance.counter. > <cfset Variables.Age = #DateDiff('yyyy',BirthDate,CurrentDate)#> Same thing here. I'm assuming this is a transient variable? If it is, then it should be a function-local variable (declared with 'var'), and not in the variables scope at all. > <cfreturn Variables.Age> > </cffunction> > </cfcomponent> > > The file "myPage.cfm" contains: > <cfobject name="myObject" component="Components.mycomponent"> You're not calling the (undeclared) init method of your component to initialize it. > <cfset myBirthDate = CREATEODBCDATE('1/1/1900')> > <cfset myAge= myObject.getAge(myBirthDate)> > <cfoutput>#myAge#</cfoutput> > > > > Variables.Counter isn't necessary, but I threw it in there to illustrate the > way you would DIM a variable at the top of a VB module to make it accessible > to the entire module, or in this case, all the functions within the > component. > > How'd I do? Less important, but you don't have output="false" on your CFCOMPONENT and CFFUNCTION tags, which you should always have. You can get some weird spacing issues if you don't. cheers, barneyb -- Barney Boisvert [EMAIL PROTECTED] 360.319.6145 http://www.barneyb.com/ Got Gmail? I have 100 invites. b.n +q?N rzgu?q?q3w?? "a{ +v? ry u ^? yh~ ^zf!B o '{ 0q3w??v?y U b?? 0 -??x`7.?+[ ?Z??jGV?(m kej{Zrl ny w^ vrz 7+-Uj)ZnW 0j!o }^?? ? ---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com). CFCDev is supported by New Atlanta, makers of BlueDragon http://www.newatlanta.com/products/bluedragon/index.cfm An archive of the CFCDev list is available at www.mail-archive.com/[email protected]
