quick straw poll: thumbs up if you still use pass-...
# cfml-general
s
quick straw poll: thumbs up if you still use pass-by-reference thumbs down if you don’t (as in, don’t use it in new code)
b
Can you explain what you mean?
Complex values are passed by reference automatically so its not like I go out of my way, but it's certainly there and I use it
or are you asking if we go out of our way to use immutable/unmodifiable dataypes, like those offered by Java or BoxLang?
s
Just curious about usage as my first encounter of it was in an older project where an obj was modified without assigning it to a var as in instantiate an obj, pass it to another method and modify it afterwards all without assigning any of those operations to a var. Not sure if I'm being clear there lol
It was like
Copy code
var obj = etc
Obj.read(obj2)
Obj.otherMethod(somethingelse)
b
Right, and that's just how variables work. They don't create copies of the data, they create pointers in memory. So
obj
only lives once in the heap, but there are 1 or more variable references pointing to it
So unless
obj
is specifically an immutable class, any updates to it will be "seen" by any of the variable references pointing to it
s
Nowadays I would return something from the methods called to a var so it's just something I don't see or hadn't seen until that
I should say it wasn't until I seen that implementation that I thought about reference and value since uni!
At least until the cf team introduced instantiating structs (or arrays?) explicitly by value
b
arrays used to be passed "by value" in CF, meaning they were duplicated when being passed into a function. Lucee always passed the by reference, and I believe ACF started doing that by default a while back
Simple data types like numbers, strings, and booleans are inherently immutable in Java, and therefore in CF as well.
b
I've been using Lucee for many years now, so passing references has been second nature. Interesting that ACF did not always do that. So yes @salted, passing arrays and objects to functions happens all the time.
s
I know it happens but I hadn't ever not called a method and not assigned the result to a var
b
Fun fact, ACF will also duplicate (deep copy) all variables passed to CF thread but Railo/Lucee never has.
s
Seeing stuff mutated without any assigning threw me for a loop
b
Wow, did not know that Brad
s
Yeah I've only recently understood the usefulness and situations that require duplicate lol
Threading is also something I've never touched
b
> method and not assigned the result to a var Whether or not a method returns a value has little to do with whether it mutates the object.
Copy code
widget = new widget();
widget.disable();
There's no need for
disable()
to return anything. It simply modifies the internal state of the widget instance.
@Bill Nourse ACF did it to "help" with thread safety, but it's sort of dumb because it can cause huge issues when you pass in something like a logbox logger, and now the whole freaking coldbox framework gets deep copied 😕
b
Do you follow Lucee with Boxlang with that? I'm guessing yes
b
Yes, we do 🙂
🙌 1
I always avoid duplicating anything unless absolutely necessary
I would rather use locking or an unmodifiable data structure to ensure consistency across threads
b
Amen to that
For salted, the idea of passing references is not limited to OOP. It can be applied to any function
s
Yeah that's not quite what I mean it's more new obj, something.modify(obj) without assigning the result to a var if that makes more sense
b
Meaning the
modify()
method on the
something
class mutates the passed
obj
?
s
Yes
But there's no assignment to the result (as you don't really need to)
It's something I don't encounter much except in older codebases
Or I don't notice it often at least
The by reference / by value distinction is not something I've ever paid much attention to despite it being an important distinction in some circumstances
It's probably my lack of experience with other languages as I've used coldfusion from uni into my professional career
b
@salted:
Copy code
"... an obj was modified without assigning it to a var as in instantiate an obj, pass it to another method and modify it afterwards all without assigning any of those operations to a var. Not sure if I'm being clear there lol"
I suppose the following code illustrates what you mean. If so, then the explanation is the one that @bdw429s gave, namely:
Copy code
"...unless obj is specifically an immutable class, any updates to it will be "seen" by any of the variable references pointing to it"
<!--- testpage.cfm --->
<cfscript>
obj = new Person(email="<mailto:FirstName@first.domain.com|FirstName@first.domain.com>");
writedump(var=obj, label="Person object upon instantiation");
caller1 = new Caller1();
caller1.modify(obj);
writedump(var=obj, label="Person object after caller1.modify() call");
caller2 = new Caller2();
caller2.modify(obj);
writedump(var=obj, label="Person object after caller2.modify() call");
</cfscript>
<!--- Person.cfc: mutable! --->
<cfcomponent accessors="true">
<cfproperty name="email">
</cfcomponent>
<!--- Caller1.cfc --->
<cfcomponent>
<cffunction name="modify">
<cfargument name="inputObject">
<cfset arguments.inputobject.setEmail("<mailto:SecondName@second.domain.com|SecondName@second.domain.com>")>
</cffunction>
</cfcomponent>
<!--- Caller2.cfc --->
<cfcomponent>
<cffunction name="modify">
<cfargument name="inputObject">
<cfset arguments.inputobject.setEmail("<mailto:ThirdName@third.domain.com|ThirdName@third.domain.com>")>
</cffunction>
</cfcomponent>