Tuesday 16 December 2014

F***ing StackOverflow. And new vs createObject() vs entityNew()

G'day:
I was typing this response on Stack Overflow, and just as I was finishing, the question was closed and I was unable to save it. What a bunch of pricks the rule-makers on StackOverflow are.

I'm not having my work wasted, so I'm publishing it here instead.

Obviously any thoughts or elaborations are welcomed.



Question:

In ColdFusion which way is better to create a component object CreateObject() or EntityNew() or New keyword


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.

My answer:

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). [thanks to Scott for reminding me about that].

Oops... someone else has closed this question. I guess this might be a waste of time!

That's it.

The question has been cross-referenced to this one: "Differences between New/CreateObject() vs EntityNew() in CF9 for persistent CFC?". It answers the same question, but very nebulously (kinda appropriately for the general tenor of the question).

--
Adam