Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Duplicate() function help protect data integrity?

Tags:

coldfusion

  1. I would like to understand the purpose of the Duplicate() function when used on a variable before it's returned by a function. It would be nice if you could provide an example of when the use of Duplicate() is important or necessary.
  2. Does it have other purposes?

I have heard others mention Duplicate() in the context of data integrity, and I would like to get a better understanding of how it works and when to use it.

I often see this:

<cfreturn Duplicate(local.myVariable)>

When this also returns the same result:

<cfreturn local.myVariable>
like image 950
Mohamad Avatar asked Dec 24 '10 20:12

Mohamad


1 Answers

While most variables are passed by value in ColdFusion, Structures are not -- they are passed by reference, which can cause unintentional overwriting of values, if you aren't careful.

Using Duplicate() creates an identical copy of the variable in another location in memory, so that when it is passed by reference (note that you are still passing by reference, there is no way to force a pass by value) any changes made to the data from the caller do not affect the source of the return.

like image 60
Adam Tuttle Avatar answered Oct 24 '22 01:10

Adam Tuttle