We've been discussing the upgrade of Legacy Code. For our purposes, this is outdated ColdFusion code, often written against long retired versions of the ColdFusion server, that is still running out in production today. Often a lot of time, money, and effort went into creating these applications, and they were great, which is why so many still exist today. Unfortunately, they also haven't been given the love and care they've really needed, over the years, and now they're bulky, sluggish, and full of security flaws. We've already discussed several first steps towards updating these outdated applications. These were all big steps, so now it's time to focus on the smaller tasks needed.

Every application requires general maintenance, bug fixes, adjustments, and improvements. Going forward, most of the changes you will make will come in the course of this type of work. You'll make changes as you write a new fix, add some field to a form, or make some layout adjustment. You can make small projects, to address each coming task incrementally and proactively, but some organizations just don't have the time or resources, especially if your's is a large application. So you pay off your code debt in small, digestable chunks, within the confines of ongoing work.

A great example of this is our next task: scoping all of your variables. It was not uncommon for developers to write code as they read it out of the CFWACK, and much of that sample code was largely unscoped. Obviously it still worked, as those applications are still around, but most every ColdFusion developer has run into variable bleedover (two variables of the same name interfering with each other) at one time or another.

With smaller applications the performance improvement, of scoping all of your variables, will not typically have a huge impact. On the other hand, for large, high traffic applications, you can see measurable improvement. When ColdFusion processes each request, and finds an unscoped variable, it runs a background method (ScopeCheck) to walk through a hierarchy of scopes to search for the variable being referenced. Now, imagine a lengthy (and popular) dynamic template with 30 or 40 unscoped variable references. Now imagine that same template getting several thousand concurrent requests. That's a lot of ScopeCheck going on!

By explicitly declaring each and every variable with it's scope prefix, you eliminate all of this background process from occurring. The server doesn't have to search through the scopes to "find" the variable in question, because you've told the server exactly where it's located.

In my next post we'll talk more about proper scoping, but to get started it's important to know that A) you need to, and B) that you scope every variable. Even those in the VARIABLES and LOCAL scopes should be prefixed. It will prevent the server from unnecessarily searching for the variables, and reduce overall code complexity, because a variable's scope will never again be in question.

This article is the ninth in a series of articles on bringing life back to your legacy ColdFusion applications. Follow along in the Legacy Code category.