<p>Been going over my predecessor's code and see usage of the "request" scope frequently. What is the appropriate usage of this scope?</p>

[{'answer_id': 26725, 'author': 'Adam Tuttle', 'author_id': 751, 'author_profile': 'https://Stackoverflow.com/users/751', 'pm_score': 5, 'selected': True, 'text': '<p>There are several scopes that are available to any portion of your code: Session, Client, Cookie, Application, and Request. Some are inadvisable to use in certain ways (i.e. using Request or Application scope inside your Custom Tags or CFC\'s; this is <a href="http://en.wikipedia.org/wiki/Coupling_%28computer_science%29" rel="noreferrer">coupling</a>, violates encapsulation principles, and is considered a bad practice), and some have special purposes: Cookie is persisted on the client machine as physical cookies, and Session scoped variables are user-specific and expire with the user\'s session on the website.</p>\n\n<p>If a variable is extremely unlikely to change (constant for all intents and purposes) and can simply be initialized on application startup and never written again, generally you should put it into Application scope because this persists it between every user and every session. When properly implemented it is written once and read N times.</p>\n\n<p>A proper implementation of Application variables in Application.cfm might look like this:</p>\n\n<pre><code>&lt;cfif not structKeyExists(application, "dsn")&gt;\n    &lt;cflock scope="application" type="exclusive" timeout="30"&gt;\n        &lt;cfif not structKeyExists(application, "dsn")&gt;\n            &lt;cfset application.dsn = "MyDSN" /&gt;\n            &lt;cfset foo = "bar" /&gt;\n            &lt;cfset x = 5 /&gt;\n        &lt;/cfif&gt;\n    &lt;/cflock&gt;\n&lt;/cfif&gt;\n</code></pre>\n\n<p>Note that the existence of the variable in the application scope is checked before and after the lock, so that if two users create a race condition at application startup, only one of them will end up setting the application variables.</p>\n\n<p>The benefit of this approach is that it won\'t constantly refresh these stored variables on every request, wasting the user\'s time and the server\'s processing cycles. The trade-off is that it is a little verbose and complex.</p>\n\n<p>This was greatly simplified with the addition of Application.cfc. Now, you can specify which variables are created on application startup and don\'t have to worry about locking and checking for existence and all of that fun stuff:</p>\n\n<pre><code>&lt;cfcomponent&gt;\n    &lt;cfset this.name = "myApplicationName" /&gt;\n\n    &lt;cffunction name="onApplicationStart" returnType="boolean" output="false"&gt;\n        &lt;cfset application.dsn = "MyDSN" /&gt;\n        &lt;cfset foo = "bar" /&gt;\n        &lt;cfset x = 5 /&gt;\n        &lt;cfreturn true /&gt;\n    &lt;/cffunction&gt;\n&lt;/cfcomponent&gt;\n</code></pre>\n\n<p>For more information on Application.cfc including all of the various special functions available and every little detail about what and how to use it, <a href="http://www.raymondcamden.com/2007/11/09/Applicationcfc-Methods-and-Example-Uses" rel="noreferrer">I recommend this post on Raymond Camden\'s blog</a>.</p>\n\n<p>To summarize, request scope is available everywhere in your code, but that doesn\'t necessarily make it "right" to use it everywhere. Chances are that your predecessor was using it to break encapsulation, and that can be cumbersome to refactor out. You may be best off leaving it as-is, but understanding which scope is the best tool for the job will definitely make your future code better.</p>\n'}, {'answer_id': 27498, 'author': 'Dane', 'author_id': 2929, 'author_profile': 'https://Stackoverflow.com/users/2929', 'pm_score': 4, 'selected': False, 'text': '<p>This is a very subjective question, and some would even argue that it is never "appropriate" to use the request scope in modern ColdFusion applications.</p>\n\n<p>With that disclaimer out of the way, let\'s define what the request scope is and where it would be useful.</p>\n\n<p>The request scope is the absolute global scope in a single ColdFusion page request.  It is not a shared scope, like application, server, client, and session scopes, so locking is not necessary to make it threadsafe (unless you spawn worker threads from a single request using CF8\'s CFTHREAD tag).  As a global scope, it is a very convenient way to persist variables through any level in the request\'s stack without having to pass them from parent to caller.  This was a very common way to persist variables through nested or recursive Custom Tags in older CF apps.</p>\n\n<p>Note that while many applications use this scope to store application-level variables (configuration settings, for example), the big (and sometimes subtle) difference between the request scope and the application scope is that the value of the same request-scoped variable can differ between individual page requests.</p>\n\n<p>I would guess that your predecessor used this scope as a means to conveniently set variables that needed to survive the jump between encapsulated or nested units of code without having to pass them around explicitly.</p>\n'}, {'answer_id': 3463290, 'author': 'RandomCoder', 'author_id': 417786, 'author_profile': 'https://Stackoverflow.com/users/417786', 'pm_score': 0, 'selected': False, 'text': '<p>Okay, I just wanted to comment on your code. Please forgive me if I seem crazy. But you already verified that the structKeyExists in the beginning. Since you know it\'s going to be true, it wouldn\'t make sense to run another check. So my version of it would be this... But thats just me.</p>\n\n<hr>\n\n<pre><code>&lt;cfif not structKeyExists(application, "dsn")&gt;\n    &lt;cflock scope="application" type="exclusive" timeout="30"&gt;\n            &lt;cfset application.dsn = "MyDSN" /&gt;\n            &lt;cfset foo = "bar" /&gt;\n            &lt;cfset x = 5 /&gt;\n    &lt;/cflock&gt;\n&lt;/cfif&gt;\n</code></pre>\n\n<hr>\n\n<p>Alright. </p>\n\n<p></p>\n'}, {'answer_id': 7562153, 'author': 'Fitz', 'author_id': 965936, 'author_profile': 'https://Stackoverflow.com/users/965936', 'pm_score': 0, 'selected': False, 'text': "<p>I've been writing my company's framework that will be used to power our site.</p>\n\n<p>I use the request variable to set certain data that would be available to the other CFC's I had to do this so the data would be available throughout the application, without the need to continually pass in the data. I honestly believe that using request , and application as long as its a static function component then you should not have a problem. I'm not sure if I am wrong in my thinking with this but once I release the framework we will see.</p>\n"}]
['https://Stackoverflow.com/questions/25672', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']