Tags vs Cfscript in ColdFusion and Lucee
Feb 1 |
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
Examples
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. --->
<cfscript>
serverLanguage = queryNew("label,value","varchar,varchar",
[
{label="Adobe ColdFusion",value="ACF"},
{label="Lucee", value="Lucee"}
]);
</cfscript>
<cfelseif source eq 'db'>
<!--- Or, get the data from the db --->
<cfquery name="serverLanguage" datasource="#dsn#">
SELECT
label,
value
FROM ServerLanguages
</cfquery>
</cfif>
<cfreturn serverLanguage>
</cffunction>
Background
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.
I prefer tags for one major reason- I am a full-stack developer and the majority of my code is in JavaScript. According to GitHub, the percentage of code that used to drive this blog, Galaxie Blog, has only 18% of ColdFusion code. The rest of the code is primarily JavaScript and CSS. This is typical of most of my projects- I use tags to easily differentiate my server-side logic and JavaScript code.
Arguments for cfscript
Most developers either use or are comfortable with C-style languages. C-style languages include other popular web languages such as JavaScript, .Net, and Java.
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"/>'.
cfscript also fully supports many of the syntax abbreviations available in other languages, such as "==", "!=", "<.", "<=", ">", ">=". These abbreviations are not available with the tag syntax.
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.
Conclusion
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.
As we have already covered- one of the major differences is that the tag syntax is similar in appearance to HTML, whereas the cfscript style looks similar to JavaScript and other C-style languages.
If you're using tags, a developer can quickly differentiate between the JavaScript and the server-side code, and with cfscript, other developers that use other C-style languages may come in to better support your code. With cfscript, it will also be easier to port your code to a different language.
Better yet- you can mix and match and use both!
In the end, what matters most is your own personal preference.
Further Reading
- All hail the new operator (Ben Nadel)
- Script vs Tags and My Coding Methodology (Ben Nadel)
- I am not a fan of cfscript (Ben Forta)
- Oh Dear, Tags vs Script (Adam Cameron)
- Callbacks Function Expressions (Adam Cameron)
- Building ColdFusion for the Web
Tags
ColdFusion, ColdFusion Tag Syntax, CfscriptThis entry was posted on February 1, 2024 at 10:41 PM and has received 1005 views.