I was writing some code in ColdFusion using CFC components, when I got bit confused on using different ways to create a object of a component.
I would appreciate if someone please let me know which method of creating a object is better.
CreateObject()
, EntityNew()
& New
keyword.
I read through few blogs and got mixed answers, some said Entity New is faster as compared to Create Object.
I also found that the syntactical different is better in EntityNew()
.
I would appreciate if I can get some thoughts on from anyone.
Thanks.
Original version, written whilst this question was closed: http://blog.adamcameron.me/2014/12/fucking-stackoverflow-and-new-vs.html
Transcript: This is a very nebulous question, and I am going to vote to close it once I post this.
Unless someone can come up with real-world performance penalties using new, createObject()
, <cfobject>
or <cfinvoke>
, I'd not bother listening to them. There will not be a meaningful difference. They are dwelling in the realms of micro (and premature ~) optimisation.
All those options I list above are slightly different to entityNew()
, which is specifically intended for creating ORM-based objects. The others are more generic. But, again, performance-wise there will be no real-world consideration here.
I do not use tags when I can avoid it. So that discounts <cfobject>
and <cfinvoke>
for consideration IMO.
All of this is opinion though.
As I said, entityNew()
is specifically designed for creating ORM objects, so there might be something to be said to using that in a mixed environment that has a mix of ORM-based and vanilla objects.
As for createObject()
and new? I now reserve createObject()
for Java objects, and use new for CFML objects. For code clarity.
Also bear in mind that new also calls the init() method (or whatever the initmethod attribute on the component suggests should be called).