VARIABLES unlimited ColdFusion: performance and readability?

In my ColdFusion code, I'm used to always treating a variable region as the default region (i.e. when another region is not suitable). I understand that this improves efficiency because the ColdFusion processor does not have to waste cycles defining the area in which the variable is contained. However, I was always annoyed by how much this circumstance makes my code.

So, my question, realistically, is clearly looking at every variable behind verbosity? I understand that every application has different definitions of “worth”, but I just would like to get some opinions from more experienced developers. Do you always use every variable? For production code only? Do you find code with clearly visible variables more readable, and does the code with increased readability make it more “beautiful” (due to the lack of a better word)? Thanks in advance for any answers.

+6
source share
4 answers

There are two things to consider when defining a scope:

  • readability
  • Bleeding and performance

For both reasons, I would (generally) recommend you to cover all areas of application both in the CFC class and outside it. You will never be surprised at the results if you do this. However, these are two different cases:

Inside CFC:

ALWAYS a Variables region, since it is a "protected" region shared between methods and the body of the class. And use it only when you are very sure that you want to use it. You can use it as a property element for instantiated classes, but be very careful if you use it in a singleton class (cached in the Application scope). This can lead to unintentional bleeding. In addition, if there are any external variables with the same name, you can again inadvertently pull them out of thin air. Not what you want. In addition, the uncertainty of the Variables region in CFC is confusing since there are local var values ​​that do not accept the region prefix. Just the Variables area is here if you use it here.

Outside of CFC:

You can allow yourself to not cover Variables on the CFM / include page, as long as it is clear, imho. Efficiency does not have to be effective since CF will check this FIRST area (outside of CFC). My general preference is that I first install / declare it, and then, if it is clear and in a visual context, leave it without access. If the page is very long or I broke the page (for example, a long report, for example) into pieces (including), I will definitely make it prefix again, so it’s very clear where it comes from. If I consistently cover all other areas (which I recommend), it should be clear that unscoped is Variables , but I like it. In particular, if other people are also working on a codebase. Therefore, if it is not clear, open it. Good rule of thumb.

Search Order:

Finally, I hate it when people talk about search order and then mix CFC and page areas. Keep in mind that they are different, and CF did not document them very well or clearly ( Local and var are equal "where the last time wins, Arguments will be found before Local in CFC, when undeclared or overloaded, Variables and Property also mixed). On the page (CFM / include template), the Variables area is the default and the search is performed first. The exception is the request area in the context of the request block. Again, they are not very well documented, and in CF we don’t always think of block coverage, as in some languages but it really is. Out of context of request expect Variables Variables for trump cards. If you are not sure about this, check it out or check it. Also keep in mind that the this , Request , Application and Session fields are not executed if the CF encounters a non-prefix variable.

+3
source

The target part of the answer (so people don’t vote to close it as “too subjective”) is that there is an expected performance from always varying coverage. However, I heard some - anecdotal - assumptions that in ColdFusion (unlike Railo) variables with scope are not faster than faster.

In the real world: the difference is not significant.

I would be mistaken in clearing the code and reading easier, and in the end I talk only about the scope variables of the scope, when necessary.

Note. I always use view variables, URLs, etc. variables, even if CF "finds" them without a frame. It seems messy not to cover these (again, subjective, and only IMO in this case).

+4
source

I worked on many CFML projects, and I saw that developers are engaged in literally everything, in some area there is nothing (do not do this!), And others are everything except the field of variables. Although defining the scope is important, I find that defining variables in the scope of variables is redundant, inflates the code, and provides little to no increase in performance for your application.

I would say everything pops up except the variable area. This will make it easier to read your code base. Focusing on other areas of your application, such as good caching techniques, indexing databases and query optimization, minimizing browser HTML / CSS / JS resources, etc., will help to make noticeable differences in how well your application works.

+1
source

I prefer to cover everything, but I find that the scope of variables is annoying (the name of a single letter area would be nice, for example, v. Yes, I know that sounds silly.) I tend to think that the scope gives readability.

I (after some time from the page) or another developer looking at my code, you just need to see #form [...] # to know that this part of the page depends on the # form of # variables. It is especially convenient in cases where there are [url and / or form] and query columns with the same name (for example, url.userID, form.userID, getUserInfo.userID).

In addition, it is natural that cold fusion has the preferred order for how it finds variables without visible regions.

+1
source

Source: https://habr.com/ru/post/975932/


All Articles