Question

I am using ColdFusion 8 and 9.

I was reading some ColdFusion best practices yesterday. I came across a section that advised to always use CFSCRIPT whenever possible. The reasoning was that each CFSET is a separate request and needs to be opened and closed by the server (or something to that effect). Since CFSCRIPT is handled as a single block of code, only one open and close is necessary, thereby taking less time for the server process. This advice was given explicitly for ColdFusion 5.

I am wondering if this advice holds true today, almost a decade later. Personally, I use CFSCRIPT 99% of the time. I do, however, work with people who use only tags. In a project that I am working on, I encountered this code within a function:

<cfset LOCAL.TrackingInfo = structnew()>
<cfset LOCAL.TrackingInfo.referral = CGI.HTTP_REFERER>
<cfset LOCAL.TrackingInfo.ipaddress = CGI.REMOTE_ADDR>
<cfset LOCAL.TrackingInfo.useragent = CGI.HTTP_USER_AGENT>
<cfset LOCAL.TrackingInfo.querystring = CGI.QUERY_STRING>

I would modify it to look like this:

<cfscript>
    LOCAL.TrackingInfo = structNew();
    LOCAL.TrackingInfo.referral = CGI.HTTP_REFERER;
    LOCAL.TrackingInfo.ipaddress = CGI.REMOTE_ADDR;
    LOCAL.TrackingInfo.useragent = CGI.HTTP_USER_AGENT;
    LOCAL.TrackingInfo.querystring = CGI.QUERY_STRING;
</cfscript>

For me, the benefit is readability. And, it's really easy to go back and forth between CFSCRIPT and JavaScript and Java (the little Java that I have done).

Is there a tangible readability or performance benefit to using CFSCRIPT? Is there any reason for a non-beginner to continue using CF tags?

Was it helpful?

Solution

No real speed difference to speak of in modern versions of the language. As far as readability that is completely subjective to who is doing the reading. I work with a wide array of CF people. Some of those people have a really hard time following CFSCRIPT and others that is all they ever try to use(almost to the point of overkill). Personally I say go with what ever approach you are most comfortable with and is also an approach whatever team of people you are working with is acceptable. I think the ultimate goal here is to produce code that everyone in the team can easily understand and support if ever needed. An example of readability arguments could be that I know someone here that would say your CFSCRIPT block is not as readable as this:

<cfscript>
    LOCAL.TrackingInfo  = { referral = CGI.HTTP_REFERER, 
                            ipaddress = CGI.REMOTE_ADDR, 
                            useragent = CGI.HTTP_USER_AGENT, 
                            querystring = CGI.QUERY_STRING };
</cfscript>

I do not think though either one of you is "more right" than the other. Just differences in personal coding styles.

Or another example I work with one person who insists that all CF tags be in capital letters along with attributes. Their stated reason is it helps in readability. I do not find it any easier or harder to read when it is in all caps. If anything I find it just a hassle to always hold down the shift kit or hit the caps lock when messing with code on those specific projects.

OTHER TIPS

When this was posed to the CF team it was stated there is a negligible difference in using cfscript, but it is so small that no one should ever worry about it. This comes the way the cfscript functions were written (in tags) so there is a tiny cost associated with the extra function call. If you notice a difference in any sort of load or speed test I'd be surprised.

Quick Edit: The stuff you are using in your example above there would be no difference, the statement I am making is only in regards to the new cfscript language enhancements in CF9.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top