Unless there is some great need to use a single line of code to perform
multiple actions, don't do it. You'll thank yourself 6 months down the road
when you come back to the code and don't have to spend that extra time
wondering what the heck you were thinking.
<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.
Here's my favorite example of a single line that does everything (the first
4 are setup), translated from Java to CF. Of course, it won't actually run
on CF, becuase CF doesn't have as feature rich a 'for' construct as Java,
but you can see the point. And it's probably not exactly right anyway,
because I had to rebuild it from memory. It reverses an array, which isn't
immediately obvious.
a = arrayNew();
for (i = 1; i LTE 10; i = i + 1)
a[i] = i;
for (i = 1, t = a[1]; i LTE floor(arrayLen(a) / 2); i = i + 1, t = a[i],
a[i] = a[arrayLen(a) - i + 1], a[arrayLen(a) - i + 1] = t) {}
Just because you can, doesn't mean you should.
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 Sparrow-Hood, Walter
> Sent: Friday, August 15, 2003 2:57 PM
> To: [EMAIL PROTECTED]
> Subject: RE: [CFCDev] CFC Persistance
>
>
> Is there any place method chaining using 'this' is further explained - I
> just got very confused!!
>
> Sean, will you be covering things like this in your upcoming CFMX 6.1 Best
> Practices?
>
> Walt
>
>
>
>
> -----Original Message-----
> From: Sean A Corfield [SMTP:[EMAIL PROTECTED]
> Sent: Friday, August 15, 2003 12:35 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [CFCDev] CFC Persistance
>
> On Friday, Aug 15, 2003, at 07:18 US/Pacific, Brad Howerter wrote:
> > Why would you return 'this' from the init function? 'This' is not a
> > pointer
> > to the object, so what good does it do the method caller?
>
> Method chaining. It allows the following convenient idiom:
>
> <cfset obj = createObject("component","mycfc").init() />
>
> "this" *is* a reference to the object (not a pointer - there are no
> pointers in Java or CF).
>
> It can be very convenient to have all mutators (setters, initializers)
> return "this" (instead of returning nothing - returntype="void"). Then
> you can do:
>
> <cfset obj.setFirstName("Sean").setLastName("Corfield") />
>
> Also note that omitting returntype= is *not* the same as specifying
> returntype="void". The latter ensures that your function returns
> nothing (i.e., you have no cfreturn tag or only empty cfreturn tags).
> If you simply omit the returntype= attribute, you can return anything
> and it is unchecked.
>
> 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).
> ----------------------------------------------------------
> 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).