ColdFusion has many different scopes for variables. Some of the major ones include Application, session, client, 
variables, request, cookie, FORM, URL, and there are others.

When setting and reading variables, you should always prefix the variable name with the appropriate scope.

For example, if you submit a form with a 'name' field, the action page will have access to that variable
as either 'name' or 'FORM.name'.

It isn't necessary to supply the 'FORM' part of the variable name, but there are three reasons you should.

1. It is a best practice - you should just do it!

2. It is easier to read - Requirements tend to grow larger and more complex, and your pages will need additional code to handle the increased complexity. If your variables aren't scoped, you can lose site of where the variable comes form.

3. The page will run faster - If all variables are scoped, the CF server knows where to look for the variable when reading them. If your variables don't have a scope, the server has to look around in memory until it finds it.

These rules apply to all scopes, not just FORM variables.

About This Tutorial
Author: Nathan Miller
Skill Level: Beginner 
 
 
 
Platforms Tested: CF4,CF5,CFMX
Total Views: 109,565
Submission Date: May 19, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 11
Discuss This Tutorial
  • I've found that there are a few good occasions where it's best not to prefix my variables. When writing a query, if I don't prefix my variable in the WHERE clause, I can route a URL and/or FORM variable to it and it works for both. Like this : WHERE id = #ID# If I were to prefix it like below, I'm locking myself into routing only one type of scope to it, and others would cause an error. WHERE id = #URL.ID#

Advertisement


Website Designed and Developed by Pablo Varando.