onDuplicate() for ColdFusion 9
I just submitted an enhancement request for ColdFusion 9 and I hope this will receive lots of community support.
With the addition of the ability to duplicate() CFCs and the fact that cfthread will do this automatically, we now have something of a hidden problem with ColdFusion. It's the proverbial "waiting to happen" problem. You've got a CFC and you're duplicating it and everything is fine and then suddenly you find some reason to want to have it contain an internal pointer to a caching object or an IoC factory and BLAM! Suddenly you've got a major memory leak in your application where large numbers of singletons (objects that should only be created once) are being duplicated willy-nilly.
The problem isn't that it *can* happen of course, because there are plenty of ways to screw up any piece of software. What got me started down this road is that it occurred to me that
- it's invisible: it's the sort of problem that's easy to not see coming, because the pieces of code involved are in very different places -- you set the pointer to the IoC factory in your init() method, but then your cfthread tag or your duplicate() call is somewhere else entirely.
- there's a simple and effective solution: the server can check for the existence of an onDuplicate() method and if present use the returned value instead of a deep-clone of the object, making duplicate() an at least semi-intelligent cloning engine.
How does this solve the problem? Very simple. Authors of IoC factories, caching engines and other memory-sensitive components can protect their objects from accidental duplication like this:
<cffunction name="onDuplicate" access="public" output="false">
<cfreturn this />
</cffunction>
</cfcomponent>
With the simple addition of that one function then the author knows that those objects will never be accidentally duplicated, because any attempt to clone them with duplicate() will result in returning a "this" pointer to the public object. Of course there are other possible applications for this feature (just as there are many applications for onMissingMethod), although in my mind this is the most vital.
So as you're looking forward to ColdFusion 9 and hearing people talk about their favorite enhancement requests, be sure to mention onDuplicate(). :)
Thanks,
ike
p.s. One other place that occurred to me where this might be useful is for setting "constants". ColdFusion has never had any concept of a "constant" and even this wouldn't be a true constant, however, if you had a collection of "constants" you wanted in your application you could create a simple "bean" object containing your constant values as private variables, use an onMissingMethod() to get access to them and an onDuplicate() to prevent their being coppied.
<cfset pi = 3.14 />
<cfset c = "6000km/sec" />
<cffunction name="onMissingMethod" access="public" output="false">
<cfargument name="MissingMethodName" type="string" required="true" />
<cfreturn variables[arguments.MissingMethodName] />
</cffunction>
<cffunction name="onDuplicate" access="public" output="false">
<cfreturn this />
</cffunction>
</cfcomponent>
<cfset area = constants.pi() * (radius^2) />
<cfset e = mass * (constants.c()^2) />

There are no comments for this entry.
[Add Comment]