Hi Jason,
My first thought is, "why are you worrying about memory space
allocation?" Do you expect to run into problems? Even if there is a
difference,
it is likely to be very minute, as it appears you are storing the same
information in the same manner either way. The only difference is, in
one way you are setting the variables to stringNull, which I presume
would likely be smaller number of bytes than something the client code
would put into the init function. But, my guess is that doesn't matter,
as those values are likely to not remain null.
Two other thoughts I had, for which you did not ask opinions (but I will
give anyway =)) :
1) As I see the addressTypes variable being used, it does not appear to
be properly scoped. Did you intend this?
2) Regarding the use of application.stringNull - I don't think many
think it is good practice to have your cfc's rely on specific variables
in the global scopes like that. Ray Camden's "Scopes in a CFC" pdf
(http://ray.camdenfamily.com/downloads/cfcscopes.pdf) mentions that "in
general you should not refer to these scopes inside a CFC. When you do
you are making your CFCs less portable between applications."
Personally, I feel you shouldn't rely on specific variables in these
scopes - since I see nothing wrong with relying on a scope itself, if it
will always be there, in that light.
If you need to, I might prefer to put that in the request scope, and
cfinclude a config file which sets it to be sure it is always there.
Although, I must say I haven't thought at length about the differences
between the two, so I could be wrong.
And, out of curiousity, what is the value of application.stringNull?
Following the principle of least surprise, I would expect it to be "",
but then I wondered why you would bother putting it in a variable? Might
it change?
-Sam
Jason Davey wrote, On 11/1/2006 3:14 PM:
Hello:
Can anyone tell me if there is a difference in memory space allocation
in the following two example implementations on initializing component
properties? The reason I ask, is that I'd prefer to call the init()
method from within the construction code but don't want to run into
variable memory allocation debugging down the line when the component
gets complex.
Example 1: set component properties in the pseudo-constructor space
between the <cfcomponent... tag and the first function using inline code.
<cfcomponent displayname="Address Class" hint="Address Class">
<!--- Property constructor for this class --->
<cfscript>
addressTypes = "company,other,home,mailing,form";
variables.props = StructNew();
variables.props.addressStreet = application.stringNull;
variables.props.addressCity = application.stringNull;
variables.props.addressState = application.stringNull;
variables.props.addressPostalCode = application.stringNull;
variables.props.addressCountry = application.stringNull;
variables.props.addressType = application.stringNull;
</cfscript>
Example 2: set component properties in the pseudo-constructor space
between the <cfcomponent... tag and the first function calling a
init() function.
<cfcomponent displayname="Address Class" hint="Address Class">
<!--- Property constructor for this class --->
<cfscript>
init();
</cfscript>
...
The init() function…
<cffunction name="init" access="public" returntype="address"
output="false" displayname="" hint="I set values to address instance
properties.">
<cfargument name="argStreet" type="string" required="false"
default=application.stringNull/>
<cfargument name="argCity" type="string" required="false"
default=application.stringNull/>
<cfargument name="argState" type="string" required="false"
default=application.stringNull/>
<cfargument name="argPostalCode" type="string" required="false"
default=application.stringNull/>
<cfargument name="argCountry" type="string" required="false"
default=application.stringNull/>
<cfargument name="argType" type="string" required="false"
default=application.defaultAddressType/>
<cfscript>
addressTypes = "company,other,home,mailing,form";
variables.props.addressStreet = arguments.argStreet;
variables.props.addressCity = arguments.argCity;
variables.props.addressState = arguments.argState;
variables.props.addressPostalCode = arguments.argPostalCode;
variables.props.addressCountry = arguments.argCountry;
variables.props.addressType = setAddressType(arguments.argType);
return this;
</cfscript>
</cffunction>
Thanks for any thoughts,
Jason D.
You are subscribed to cfcdev. To unsubscribe, please follow the
instructions at http://www.cfczone.org/listserv.cfm
CFCDev is supported by:
Katapult Media, Inc.
We are cool code geeks looking for fun projects to rock!
www.katapultmedia.com
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]
You are subscribed to cfcdev. To unsubscribe, please follow the instructions at
http://www.cfczone.org/listserv.cfm
CFCDev is supported by:
Katapult Media, Inc.
We are cool code geeks looking for fun projects to rock!
www.katapultmedia.com
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]