Recently I have been working on a ColdFusion application I inherited which only uses components in a non "Object Orientated" fashion. While I was refactoring the logic I noticed that anytime a component method called another method it used the "<cfinvoke" tag and that the component was explicitly named even if the method was in the same component as the calling method.
This got me thinking. Does explicitly naming the component in the cfinvoke call change the behaviour of the actual logic? (This is ignoring when we explicitly create objects.) There are two ways of looking at this:
In order to figure which is the actual behaviour out I wrote this simple test consisting of a component and a index file.
First: #variables.testvalue#
Last: #variables.testvalue#
#arguments.label# #variables.testvalue#
For the sake of being thorough I ran the test twice, first by calling the component transiently and the second time by creating an object instance and calling the runTest method.
Here is my test index.cfm file.
CFC Instationion Test
As you can see from the results below, when we specify the component attribute a new transient instance of that component is created in memory. This instance has its own memory and has no knowledge of the values in the calling components memory space.
When you think about it this makes perfect sense. If ColdFusion see's a component attribute in the cfinvoke tag then it will automatically load a new instance of the component as the process of checking if the component is the same as the caller could incur additional unneeded overhead.
CFC Instationion Test
First: 1
Component specified, no increment. Expected value = 1: Actual = 1
Component specified, variable incremented. Expected value = 2: Actual = 1
Component not specified, variable incremented. Expected value = 2: Actual = 2
Last: 2
In the app I am working on this behavior has no functional bearing as all components were called transiently and no values are stored in the components memory space. However had a more object orientated coding pattern been used this could have led to some very strange behavior.
From a performance point of view this behavior means that extra class loading was being incurred and so removing the component attribute is an obvious choice to improve performance.