Why did I do that?

Yesterday someone emailed me asking about the assignCategory method of BlogCFC. Specifically, they wanted to know why I did this:

<FONT COLOR=MAROON><cfset var checkEC = <FONT COLOR=BLUE>""</FONT>></FONT>

Where checkEC was defined as a query a few lines later. They noticed I did this throughout my code and were not sure why. I explained to them about how scoping works in UDFs. Any variable created in a UDF will automatically “leak” out of the UDF. (Note, when I say UDF, I also mean CFC method, as this applies to both.) This is easy enough to fix by using the VAR scope within your function.

However, you shouldn’t forget that some tags act just like cfset. So for example, my cfquery tag creates a variable as well. By var scoping the name of the query, I ensure that the data doesn’t escape the method.

So for most readers, this is old news, but it certainly can’t hurt to repeat - as forgetting to var scope can lead to serious issues down the road.

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.

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

Comments