Last week, there was a conversation in the #coldfusion channel on Dalnet IRC where one person was pondering if the ‘passed by reference’ behavior of ColdFusion (and just about every other programming language) ‘breaks’ encapsulation when using object oriented (OO) design patterns or architecture.
For those who may not understand what ‘passed by reference’ means, it is a way that programming languages handle complex data types (such as structures, queries or CFC instances in ColdFusion) to save memory. When you create a structure in ColdFusion, it gets stored in memory. Whenever you need to reference this structure or pass it to a function or custom tag, ColdFusion uses a ‘pointer’ to show where in the memory this variable is stored, rather than another instance of the variable. Any changes made to the structure in the function or custom tag, will be reflected in the ‘original’ structure.
Here is some sample code to show how this works.
<cfset struct1 = structNew() /> <cfset struct1.name = "New York Giants" /> <cfset struct2 = struct1 /> <cfset struct2.name = "New York Yankees" /> <cfoutput>#struct1.name#</cfoutput>
This code will actually output ‘New York Yankees’ rather than ‘New York Giants’, as some might expect.
The person who asked the question was concerned about how you can pass an object into a method, modify properties of that object, NOT return the object, yet still see those property changes reflected in the original object. He assumed this ‘broke’ encapsulation. Here is the analogy he used:
“Let’s say my method is a ‘locked room’ and I pass this method arguments by slipping paper under the door. If I pass a piece of paper under the door that represents the object, should I not get back a piece of paper with the changes made to my object?”
I liked this analogy as it represents a method as a kind of ‘black box’, which they should be, where we merely pass in data and that data gets handled and we do not need to worry about it. There were some flaws in the logic, though, as ‘passing by reference’ is not an OO principal, rather a very low level process of the programming language. When we in the channel explained that ‘passed by reference’ is not an OO principal, rather the way programming languages pass around complex data types to save memory, he then asked
“So using my door analogy, I would slide a piece of paper under the door, yet keep a copy for myself and any changes that are made by my method will automatically appear on the piece of paper I keep?”
I thought about this for a little bit as it did not seem to completely explain what was going on. After a few minutes, I think I came up with an analogy that works well (and he said helped him understand a bit more):
“Instead of sliding a piece of paper under the door that represents your object, you merely slide a note under the door that tells the method that the paper (object) it needs is tacked up on the bulletin board in the hall. You do not have that paper either, rather, you also have a note saying that the paper (object) is on the bulletin board.”
I will admit this may not be a perfect analogy either, but it did help this person and I thought it might help clarify this for others as well.