ColdFusion non-scoped vs. VARIABLES scope: perform

2019-06-17 08:29发布

In my ColdFusion code, I have made it a habit to always treat the variables scope as my default scope (i.e., when another scope doesn't fit). My understanding is that this improves efficiency, since the ColdFusion processor doesn't have to spend cycles determining the scope in which the variable is contained. However, I've always been annoyed by how verbose this makes my code.

So, my question is, realistically, is explicitly scoping every variable worth the verbosity? I understand that each application has varying definitions of "worth," but I'd just like to get some opinions from more experienced developers. Do you always scope every variable? Only for production code? Do you find code with explicitly scoped variables more readable, and does the increased readability trump "prettier" (for lack of a better word) code? Thanks in advance for any responses.

4条回答
对你真心纯属浪费
2楼-- · 2019-06-17 09:06

I have worked on many CFML projects and I've seen developers scope literally everything, some scope nothing at all (don't do this!), and others who scope everything except the variables scope. While scoping is important, I think scoping the variables in the variables scope is overkill, bloats the code, and provides little to no performance increase for your application.

I would say scope everything except the variables scope. This will make your code base much easier to read. Focusing on other areas of your application such as good caching techniques, database indexing and query optimization, minimizing HTML / CSS / JS assets to the browser, etc are what are going to make noticeable differences in how well your application performs.

查看更多
成全新的幸福
3楼-- · 2019-06-17 09:17

I prefer to scope everything but I do find the variables scope annoying (a one letter scope name would have been nice, like v. Yes I know that sounds silly.) I tend to think that scoping lends to readability.

I (after some time away from a page), or another developer looking at my code, merely need to see #form[...]# to know that that part of the page relies on #form# variables. Especially handy in cases where there are [url and/or form] and query columns with the same name (like url.userID, form.userID, getUserInfo.userID).

Further, naturally, cold fusion has a preferential order for how it finds un-scoped variables.

  1. Function local (VAR keyword)
  2. Thread local (CFTHREAD)
  3. Query results
  4. Function ARGUMENTS
  5. Local VARIABLES
  6. CGI variables
  7. FILE variables
  8. URL parameters
  9. FORM fields
  10. COOKIE values
  11. CLIENT variables

    source: In ColdFusion, variables are resolved in what order?

查看更多
女痞
4楼-- · 2019-06-17 09:27

The objective part of the answer (so people don't vote to close it as being "too subjective") is that there is a supposed performance gain from always scoping variables. That said, I have heard some - anecdotal - suggestion that on ColdFusion (as opposed to Railo) that not scoping variables-scoped variables is actually faster.

In the real world though: the difference is inconsequential.

I'd err towards keeping the code cleaner and easier to read, to which end I say only ever scope variables-scope variables when it's necessary.

Note: I always scope form, URL etc variables even though CF will "find" them without the scope. It seems messy to not scope these ones (again, subjective, and only IMO in this case).

查看更多
做自己的国王
5楼-- · 2019-06-17 09:30

When scoping, there are two things to consider:

  1. Readability
  2. Bleeding & Performance

For both of these reasons, I would (generally) recommend you scope ALL scopes both in and out of a CFC class. You will never be surprised by the results if you do this. However, these are two different cases:

Inside a CFC:

ALWAYS scope Variables as it is a 'protected' scope shared amongst the methods and body of the class. And only use it when you are very sure you are wanting to use it. You can use it like a property member for instantiated classes, but be very careful if you are using it in a singleton class (cached to the Application scope). This may cause unintended bleeding. Additionally, if there are any outside variables with the same name, you again may unintentionally grab them out of the air. Not what you want. Additionally, not scoping a Variables scope in a CFC is confusing as there are also local var scoped values which take no scope prefix. Just scope Variables here if you use it here.

Outside a CFC:

You can afford to not scope the Variables in a CFM page/include as long as it is clear, imho. There should be effectively no performance loss as CF will check this scope FIRST (outside a CFC). My general preference is to initially scope when I set/declare it, and then, if it is clear and within visual context, leave it unscoped. If a page is very long, or I have broken a page (like a long report for instance) into chunks (includes), I will make sure I prefix it again so it is very clear where it is coming from. If I consistently scope all other scopes (which I recommend) it should be clear that unscoped is Variables, but I like it to be clear. Particularly if other people are also working on the code base. So if it is not clear, scope it. Good rule of thumb.

Order of Search:

Lastly, I hate it when people talk about order of searching and then mush together CFC and page scopes. Keep in mind they are different and CF has not documented these very well or clearly (Local and Var are 'equal' where last in wins, Arguments will be found before Local in a CFC when undeclared or overloaded, Variables and Property are intermingled as well). In a page (CFM template/include) the Variables scope is the default and is searched first. An exception is the query scope within a query block context. Again, not terribly well documented and in CF we don't always think of block scoping like in some languages, but this is effectively such a case. Outside a query context, expect Variables to trump. If you are not sure of a case, scope it or test it. Also keep in mind that the this, Request, Application, and Session scopes are not searched when CF encounters a non-prefixed variable.

查看更多
登录 后发表回答