Calling init() on the createObject() function is a reasonable use of method
chaining, because object instantion within CF is friggin' nasty.  Why oh why
isn't there a 'new' operator.  That's a CF issue, and it lacks an elegant
solution.  Unfortunately method chaining is probably the best of the breed,
but that doesn't make it 'good', just 'better than the rest'.

I don't see chaining the setFirstName() and setLastName() methods as making
the operation more atomic.  If you want atomic, write a new method, called
setName(), which takes two parameters.  You can still use the object as a
Bean, as long as you still do the basic setters.

<cfset obj.setName("Sean", "Corfield") />

the method would look like this:

<cffunction name="setName" .. >
  <cfargument name="first" ... />
  <cfargument name="last" ... />

  <cfset setFirstName(first) />
  <cfset setLastName(last) />
</cffunction>

I do things like this quite frequently, particularly for help in
instantiating new objects.  CF won't let me have multiple init() methods,
and rather than doing this:

<cfset obj.init() />
<cfset obj.setFirstName("Barney") />
<cfset obj.setLastName("Boisvert") />
...

I set up a method to save me the trouble of all those calls:

<cfset obj.init() />
<cfset obj.initialize_full("Barney", "Boisvert", ...) />

The construction is atomic, and the initialization of instance variables is
also atomic.  Not as nice as multiple constructors, but it works well
enough.

The reason chaining isn't atomic (in my view), is that you have to assume
that the setFirstName() method (which is a SETter, not a GETter) is actually
going to GET you a reference to the instance.  First, that's not a setter's
job, and second, that dependance isn't a characteristic of atoms, but of
molecules which are made up of distinct atoms that rely on each other's
behaviour.

Right or wrong, it's valid, and therefore perfectly acceptable to me, as
long as you don't work on the same project as I do.  ; )

cheers,
barneyb

---
Barney Boisvert, Senior Development Engineer
AudienceCentral
[EMAIL PROTECTED]
voice : 360.756.8080 x12
fax   : 360.647.5351

www.audiencecentral.com


> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of Sean A Corfield
> Sent: Friday, August 15, 2003 3:35 PM
> To: [EMAIL PROTECTED]
> Subject: [CFCDev] One line or two (was: CFC Persistance
>
>
> On Friday, Aug 15, 2003, at 15:14 US/Pacific, Barney Boisvert wrote:
> > Unless there is some great need to use a single line of code to perform
> > multiple actions, don't do it.
>
> It's only worth doing if it is actually clearer.
>
> > <cfset obj.setFirstName("Sean") />
> > <cfset obj.setLastName("Corfield") />
> >
> > is hardly any extra typing, and it's clear that what's happening is two
> > separate actions.
>
> Perhaps, but this sends a different 'message' to the reader:
>       obj.setFirstName("Sean").setLastName("Corfield");
> It says setting both the first and last names is (sort of) an atomic
> action, rather than two separate actions.
>
> Similarly, having init() return this allows the following:
>       x = createObject("component","mycfc").init();
> which again makes the create + initialize into a visually 'atomic'
> action.
>
> Sometimes that is clearer.
>
> > Here's my favorite example of a single line that does everything
>
> And that is indeed a good example of how not to write code. But just
> because you have an extreme example does not mean that it is *always*
> wrong to merge two lines into one...
>
> Sean A Corfield -- http://www.corfield.org/blog/
>
> "If you're not annoying somebody, you're not really alive."
> -- Margaret Atwood
>
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email
> to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev'
> in the message of the email.
>
> CFCDev is run by CFCZone (www.cfczone.org) and supported
> by Mindtool, Corporation (www.mindtool.com).
>
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.507 / Virus Database: 304 - Release Date: 8/4/2003

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

Reply via email to