Tags vs Cfscript in ColdFusion and Lucee
There has been a lot of talk about the virtues of using either tags or cfscript in the ColdFusion community. At times this topic turns into a heated conversation resembling a religious argument, but both of these language syntaxes have their purpose and other than very minor differences, both are fully supported by both Adobe ColdFusion and Lucee.
This article will discuss the history and some of the advantages and disadvantages of both language syntaxes, but in the end, it is a matter of personal choice.
Table of Contents
In the following example I have both the tag and cfscript syntax. The cfscript is inside the cfscript tags and simply creates a ColdFusion query object. In the second logical branch, ie '<cfelseif source eq 'db'> I am querying the database directly. I am keeping this simple, and am not going into further detail here, but it is important to note that a cfscript can be placed inside of the tag syntax. The Adobe ColdFusion site has a lot more cfscript examples.
<cffunction name="getBestLanguage" access="remote" returnformat="json" output="true"
hint="Returns a JSON object back to the client to populate a Kendo dropdown. This function does not take any arguments">
<cfset source = "db">
<cfif source eq 'manualQuery'>
<!--- Create a ColdFusion query using CFScript. --->
serverLanguage = queryNew("label,value","varchar,varchar",
<cfelseif source eq 'db'>
<!--- Or, get the data from the db --->
<cfquery name="serverLanguage" datasource="#dsn#">
Tags have been around since the very first version of ColdFusion was released in July of 1995. Allaire, the company that founded ColdFusion, advocated using the tags as they resembled HTML. PHP was released in June of 95, and Microsoft's server-side language, ASP soon followed in December 1996, both of which are also tag-based languages.
Arguments for Tags
Most of the CF developers with over a decade of experience learned ColdFusion using tags. The Allaire brothers who founded ColdFusion wanted a language that syntactically resembled HTML. Tags tend to look better when embedding ColdFusion logic inside client-site code.
The folks who argue that tags are old and should be replaced need to realize that many of the other web languages also use tags to this day. PHP is the most popular server-side language to this day, and it is still using tags. PHP is popular because it is open-source, however, the language is also supported by major commercial sites, such as FaceBook.
Even hard-core advocates of cfscript will admit that database operations are generally much simpler using the tag-based syntax. However, Lucee supports tag islands, giving you the best of both worlds.
When developing a ColdFusion component (cfc), using tags requires the developer to input hinting and datatype logic that is necessary to create self-documenting cfc's. This ensures that the components will be self-documenting. While you can use cfscript to create hinting and data typing, some users have reported that they don't use these features as the logic is extraneous and more cumbersome.
Arguments for cfscript
If you using cfscript- it is easier for non-ColdFusion developers to come in and support the code. I have used the argument that it is easy for .Net developers to understand my cfscript-based code when a new manager inquires about replacing ColdFusion.
Porting code from, or to, another technology, such as .Net, is also simplified. Folks wanting to migrate to ColdFusion will have an easier time using cfscript, as well as migrating ColdFusion to another technology.
Generally speaking, cfscript is more compact, and less code is required. This is especially true when setting variables and writing functions. For example, variables don't require an opening "<cfset ", and function arguments don't need to declare a datatype ('type="numeric"/>'.
Since ColdFusion 10, cfscript allows developers to use closures and function expressions. Closures are widely used with jQuery and Ajax, although they are not necessary and have not been widely adopted in the ColdFusion community. According to Adam Cameron, a prominent ColdFusion blogger, there is no clear-cut use case for closures with ColdFusion. That said, there are some rare exceptions, Ben Nadel, a wildly successful ColdFusion blogger is a big proponent of closures.
Perceptions Within the Coldfusion Community Matter
When Adobe adds functionality to ColdFusion without supporting the same functionality using tags, as they have done with operations and closures, there is a perception that Adobe is encouraging folks to migrate to cfscript.
According to Ben Forta, Allaire added cfscript to entice other developers to migrate to the ColdFusion platform. However, when Adobe decides to add functionality to one syntax, and not the other, causes uncertainty and frustration within the ColdFusion community. It would be sad if developers chose to leave the ColdFusion platform due to these uncertainties.
I hope that Adobe better communicates the reasons why they have chosen to enhance one syntax over the other- or stay completely neutral and make sure that both syntaxes are equally supported.
Other than a few minor differences, such as cfscript supporting closures and a handful of extra operators, both cfscript and tags offer nearly the same functionality in both Adobe ColdFusion and Lucee. Performance is also identical since both cfscript and tags are compiled into the same Java Byte code.
Better yet- you can mix and match and use both!
In the end, what matters most is your own personal preference.
TagsColdFusion, ColdFusion Tag Syntax, Cfscript
This entry was posted on February 1, 2024 at 10:41 PM and has received 167 views.