Ask a Jedi: Arguments in a CFC/UDF

A reader asks:

Can I assign a scoped variable to a non-scoped variable and still have it be protected in a CFC (or UDF)? Example: <cfset newvar = arguments.value> Or should I create my own custom scope? Or do I have to type in arguments.value every time I want to use the variable?

You can, if you want, copy a variable from the arguments scope. Just be sure to use the var scope though:

<cfset var localcopy = arguments.somevalue>

Obviously the normal rule of copy versus pointer applies. However - I’d probably argue against this. In my opinion, if your code is working with an argument, it should continue to refer to it by the full name. Yes, it’s more typing, but I think it helps keep the code logic a bit more clear. You may forget, for example, that localcopy came from arguments.somevalue. By being consistent and using arguments.whatever, you do not have that problem. (Ok, so it’s not a problem per se - just something that may give you trouble down the road, like in six months when you need to debug the code again.)

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments