Welcome!

ColdFusion Authors: Yakov Fain, Maureen O'Gara, Nancy Y. Nee, Tad Anderson, Daniel Kaar

Related Topics: ColdFusion

ColdFusion: Article

A Closer Look at CFScript

A Closer Look at CFScript

Allaire's esteemed guru Ben Forta introduced CFSCRIPT to CFDJ readers last year with his article "Stick to the Script" (CFDJ, Vol. 2, issue 7). Hopefully, some of you absorbed the words of wisdom from our evangelist and gave CFSCRIPT a shot, but I suspect for most of you the lesson fell to the wayside when it came time to hit the code again. I can't blame you. Paltry documentation by Allaire on CFSCRIPT coupled with most CF developers' inexperience in scripting languages makes it easy to ignore.

Most ColdFusion developers don't have a scripting background. So why take up CFSCRIPT? It can't replace conventional CF tag structure. Although a modest performance gain can be seen in some circumstances, its exclusion of CF tags significantly limits its potential functionality. What does it offer most developers who are not seasoned scripters?

I believe it can make three compelling contributions to your repertoire. First, CFSCRIPT simplifies concatenation of strings and basic mathematical manipulation, even if you know little about scripting.

Second, and perhaps most important, CFSCRIPT offers an easy way to encapsulate business logic in a single location at the top of your page. Studio's coloring of scripting helps differentiate business logic from output, making it easy to find and address. It also rests in a more native state, devoid of some of the visual overhead tags create.

The third and rarely discussed reason, at least in my opinion, is that it serves as a great introduction to scripting languages. While most of us enjoy the ease of ColdFusion, we limit ourselves by not employing JavaScript, JSP, or other scripting languages. Indeed, next year Allaire will allow the cohabitation of JSP and ColdFusion in their NEO release (sixth generation ColdFusion). Future ColdFusion developers will really need to know JSP to extract the full potential of future CF applications, and what better way to cut their teeth than inside ColdFusion.

I must confess, jumping into CFSCRIPT may be a bit intimidating. Gone are meaningful plain English tags (CFSET, CFLOOP, etc.) that made developing so easy. Also, as I mentioned earlier, Allaire's normally strong documentation provides woeful coverage of CFSCRIPT. To help you along your daunting journey into the scary forest of scripting, I've prepared a large block of CFSCRIPT employing many of the basic constructs you'll want to use. I've chosen a credit card validation routine for a shopping cart. This doesn't expose all of CFSCRIPT's operations, but should provide enough examples to give you a jump start on your own projects. (Source code for this article can be found on the CFDJ Web site, www.coldfusionjournal.com.)

To start the process I open with a CFSCRIPT tag. There are no attributes for the tag, but I don't leave the Script block empty (ColdFusion doesn't like this and returns an error). Before I even get to the credit card validation, I'm taking advantage of the fact that I've started a CFSCRIPT block and set defaults for a page that would normally have been accomplished with CFSET or CFPARAM. By moving these sets into the CFSCRIPT, I'm reducing some visual and performance overhead. I identify the section with a comment that I start with two back slashes. These comment markers only comment out a line at a time and have no ending markers to stop the commenting (a bit weird for CF developers, but old hat for scripters).

The first three events are simple sets:

j=0; local.errM = ""; check = "";
(see Figure 1).

Note: Other than comment lines, CFSCRIPT ignores carriage returns. After making each assignment you must place a semicolon before addressing another operation. CFSCRIPT doesn't require any special word to make an assignment (like var or set); simply place a variable on the left, some expression on the right, and end it with a semicolon. Missing semicolons will be the primary cause of errors when you begin CFSCRIPTing, and they're generally not obvious. This single line of code replaces the following three conventional ColdFusion tags:

<CFSET local.errM = "">
<CFSET check = "">
Hopefully, you can recognize the "visual" advantage of making these settings inside of CFSCRIPT in conventional tags.

The next block of code drives navigation links on the page, and again bears no relation to the credit card routine. As I mentioned, you can't use ColdFusion tags inside CFSCRIPT. We can, however, re-create the functionality of some tags using ColdFusion functions. In this case I need to parameterize a form variable, which may or may not have been passed. Since I can't use CFPARAM, I'll have to mimic it with the IsDefined function and a simple assignment if the formfield doesn't exist:

if (Not IsDefined("form.carType")) form.carType = "Mondial t";
This introduces our first conditional statement, a simple if. JavaScripters take note, your favorite operators (!, ==, >=, etcŠ) are not available. You have to use ColdFusion operators (such as "NOT," "IS," "GTE," etc.), which can be a bit frustrating but are necessary nonetheless. To begin the "CFIF" simply type if, followed by the condition you wish to check enclosed in parentheses. In a simple conditional check, place the assignment or action right after the check and end it with a semicolon. In this case, if the condition is met (i.e., there's no form variable), I'd like to set it to a default of "Mondial t". We'll cover the if statements in more detail later, but I wanted to demonstrate that there are workarounds for some ColdFusion tags (although in this case, CFPARAM would have been easier).

Next, I moved some of the logic I employed in the navigation scheme into the CFSCRIPT block. This specific chunk of code occupied eight lines of code in my original template, so it was a prime candidate for moving into this block of CFSCRIPT. Again, we haven't hit the credit card validation yet, I'm just moving logic normally found throughout my page into a clean and more concise environment. This particular snippet peeks at the path to see if we're looking at Ferraris, Maseratis, or Ducatis (see Figure 2). For whichever one the script will set a nav count that will position one of those silly arrows the client absolutely must have:

if (cgi.Script_name CONTAINS "Fer") {local.navCount = 1; local.title = "Ferrari";}
else {if (cgi.Script_Name CONTAINS "Mas") {local.navCount = 16; local.title = "Maserati";}
else {local.navCount = 32; local.title = "Ducati";}}
In CFSCRIPT the braces allow you to perform multiple actions instead of a single set (much the same way parentheses work in ColdFusion or mathematics). Without the braces the if statement would end with the first semicolon. After the first set of braces, we have an else statement (just like CFELSE). I actually needed a CFELSEIF, but that's not directly available in CFSCRIPT so I simply embed another if statement inside the else. This block could have been accomplished with a switch statement (very similar to CFSWITCH), which Ben Forta covered in his CFSCRIPT article.

The final block of code I want to discuss prior to launching into the credit card validation is a simple banner rotator. Again, this logic would normally appear elsewhere on my template, but I'm moving it into the CFSCRIPT block to clean up my page. This gives us an opportunity to witness a simple concatenation and arithmetic operation, which CFSCRIPT performs so well. The snippet takes the title I set in the last section of code and appends a number (from 1 to 6) based on what minute it is. This essentially rotates through six different banners (named, for example, Ferrari1.gif, Ferrari2. gif, etc.) and will be used in the content after the CFSCRIPT block.

variables.mod = local.title & (Minute(now()) MOD 6) & ".gif";
You could extend this to create a string with the link and other HTML features as well. Whenever you have to assemble long strings (such as interaction with a COM object or other third-party software), always consider CFSCRIPT as an alternative to conventional CFSETs. JavaScripters should notice that ampersands, not the addition sign, concatenate strings.

Finally we're ready to play with the credit card validation. The first thing I must do is set some defaults that I'll manipulate further downstream. I could have separated these assignments with a carriage return, but I prefer keeping the simple ones in a single line for cleanliness. Next we see a new comment tag, this time in a multiline format. This comment resembles traditional ColdFusion REM statements in that content contained between the symbols is not executed (versus the single-line comments). To begin a multiline comment we employ a backslash and an asterisk /*; to end we reverse the order and employ an asterisk and a backslash */.

/* Required: local.CardNum, local.expYear, local.expMonth. PASSED: local.errM on exception */
As the comment suggests, we're now going to step through the card number and strip out any nonintegers the client may have provided us. This event gives us our first loop. Scripters will welcome CFSCRIPT's loops, but regular Cold-Fusion developers will probably find these a tad confusing. The looping syntax in CFSCRIPT bears no resemblance to its big brother, CFLOOP, and due to the requirement that CFSCRIPT employs ColdFusion operators, it's not a direct match with JavaScript or Java loops (but is familiarly close).

Loops come in a variety of flavors in CFSCRIPT; the while loop is the first one we'll discuss. It's not the most common loop (which is the for loop), but it's the first we see in this application. The while loop inspects the conditions specified in its parentheses and executes the operations inside its braces as long as the condition is met. This is very similar, yet distinct from a do..while loop, which checks for the condition only after executing each loop, allowing a minimum of one run through the loop. In this scenario we're going to progress through each element of the credit card string the client submitted and discard any characters that are not integers.

while (Len(trim(local.CardNum)) GT 0) { if(IsNumeric(mid(local.CardNum, 1, 1))) check = check & mid(local.CardNum, 1, 1); local.CardNum = RemoveChars(local.CardNum, 1, 1);}

The condition in this situation is the length of the variable we're manipulating. It'll shrink by one position each time through the loop. The trim function discards accidental spaces the client may have provided. As mentioned earlier, the operator must be a ColdFusion operator (in this case "GT") instead of a mathematical or JavaScript one. I have a simple if inquiry that, when true, appends that integer to a new string, cryptically named check. Subsequently, the RemoveChars function strips out the character we had just checked. The same snippet could have been accomplished with a conventional for loop, which I'll discuss soon, or through an REReplace() with regular expressions (a better idea, but it doesn't afford me the opportunity to talk about loops).

The next lines of script build error messages if the card number fails to meet certain basic criteria, specifically length, expiration, and whether the first digit is appropriate for the card type specified. The only thing worth noting here is the compound conditions for the first if statement.

if((Len(local.CardNum) LT 13) OR (Len(local.CardNum) GT 16)) local.errM = local.errM & "<li>Your credit card number must be between 13 and 16 characters long, and should only contain numbers.";
As with its elder brother the CFIF tag, if statements can check for multiple conditions (ranges of values, exclusion of ranges, or different strings).

Before the template evaluates the specifics of the number, it offers an opportunity to short-circuit if any of the earlier conditions uncovers an error. This prevents unnecessary processing of a card we won't accept anyway. To do this I simply wrap the remaining logic in an if statement.

First I perform some quick math and set a couple of defaults. These numbers will be used in the loop that follows.

local.lngth = Len(local.CardNum) - 1; tempCard = Left(local.CardNum,local.lngth); newNum = "";
The local.lngth is one less than the number of integers in the card, and tempCard is the card number stripped of the last digit. CFSCRIPT's variables are case insensitive, but minding case only helps build the coding discipline required in JSP or Java. Many developers prefer to employ more descriptive variables, but I've chosen to err on the side of brevity, due to the math I want to perform. For the same reason I've left the scope off tempCard, newNum, and Step1. In production I'd scope these variables to prevent possible problems.

The i=1; establishes the variable "i" as the index or incrementing variable. The second expression defines the condition under which the loop should continue (see Figure 3). In this case, as long as "i" is less than or equal to the value of local.lngth, the loop will process its contents. The last expression increments the variable. I need to step by two through the loop, thus I set "i" equal to itself plus 2. If I didn't want the step, I'd use the expression i=i+1. The CFLOOP equivalent of this for loop would be:

<CFLOOP from="1" to="#local.lngth#" step="2">
Since some credit cards are 16 digits while others are 15, I need two different operations with an if..else operation to direct each loop iteration. In the case of Visa, Discover, and MasterCard, I need to double the digit I'm currently viewing (specifically in the "i" position) and concatenate it with the next digit in line.
newNum = newNum & (2 * Mid(tempCard,i,1)) & (Mid(tempCard,nextNum,1));
For the American Express card the next digit in line must be doubled and concatenated to the first digit:
newNum = newNum & Mid(tempCard,i,1) & (2 * Mid(tempCard,nextNum,1));

Once I've built the new number I must sum all of its digits. For this a simple for loop can iterate through each number and add it to a variable I'll call sumNum:

for(j=1; j LTE Len(newNum); j=j+1) sumNum = sumNum + Int(Mid(newNum,j,1));
Given the simplicity of the operation, I've left the braces out of this for loop. If you perform more than one operation, however, you'll need braces (just as with if..else statements).

The final step compares the last digit of the credit card against the difference of the right-most digit of sumNum from 10. If it's not a match, I create an error message to display in the content.

In a real application this template would allow the client an opportunity to correct a mistaken entry before sending the card to a financial institution for verification. This template doesn't guarantee that the card submitted is valid, but that it conforms to industry standards. Performing such checks before interacting with third-party agents not only en-hances server performance (by preventing HTTP interaction for an event that was bound to fail), it also improves your chances of completing the transaction by providing the client with an opportunity to immediately correct a mistake. For best results this should be combined with a similar routine in JavaScript, providing instant feedback (if the client has JavaScript turned on) when the client has made a faulty entry.

This same application logic in traditional ColdFusion tags occupies more than twice the number of lines as its CFSCRIPT counterpart and is more difficult to read. By translating the functionality into CFSCRIPT I've made the business logic more transparent, transportable, and refined. Indeed, when I translated this logic, I uncovered several unnecessary redundancies and found a better progression for the logic simply because the operations were unencumbered by tags.

For those ColdFusion developers without scripting experience, I'd highly recommend some experimentation with CFSCRIPT in your next application. As a rule of thumb, anytime more than three consecutive CFSETs lie in a row, you'll benefit from moving them into a CFSCRIPT block. Start slow, making simple assignments in your templates. Next, for giggles, move a loop into CFSCRIPT. Finally, go back to some old business logic module and translate it into CFSCRIPT. Not only will your code be improved, you'll have gained invaluable experience in an important language family: scripting.

Resources

More Stories By Christopher Graves

Christopher Graves is president of RapidCF, a ColdFusion development
shop in Connecticut. In his prior "life" he was a Marine Corps
officer and graduate of the U.S. Naval Academy.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


@ThingsExpo Stories
SYS-CON Events announced today that SOA Software, an API management leader, will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. SOA Software is a leading provider of API Management and SOA Governance products that equip business to deliver APIs and SOA together to drive their company to meet its business strategy quickly and effectively. SOA Software’s technology helps businesses to accelerate their digital channels with APIs, drive partner adoption, monetize their assets, and achieve a...
SYS-CON Events announced today that Aria Systems, the recurring revenue expert, has been named "Bronze Sponsor" of SYS-CON's 15th International Cloud Expo®, which will take place on November 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Aria Systems helps leading businesses connect their customers with the products and services they love. Industry leaders like Pitney Bowes, Experian, AAA NCNU, VMware, HootSuite and many others choose Aria to power their recurring revenue business and deliver exceptional experiences to their customers.
SYS-CON Events announced today that AgilePoint, the leading provider of Microsoft-centric Business Process Management software, will exhibit at SYS-CON's 2nd International @ThingsExpo which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. AgilePoint is the leading provider of Microsoft-based Business Process Management (BPM) software products, has 1,300+ on-premise and cloud deployments in 25+ countries and provides the same advanced BPM feature set as J2EE vendors like IBM and Appian for the Microsoft .NET native environment. AgilePoint customer...
The Internet of Things (IoT) promises to evolve the way the world does business; however, understanding how to apply it to your company can be a mystery. Most people struggle with understanding the potential business uses or tend to get caught up in the technology, resulting in solutions that fail to meet even minimum business goals. In his session at Internet of @ThingsExpo, Jesse Shiah, CEO / President / Co-Founder of AgilePoint Inc., will show what is needed to leverage the IoT to transform your business. He will discuss opportunities and challenges ahead for the IoT from a market and tec...
SYS-CON Events announced today that Utimaco will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Utimaco is a leading manufacturer of hardware based security solutions that provide the root of trust to keep cryptographic keys safe, secure critical digital infrastructures and protect high value data assets. Only Utimaco delivers a general-purpose hardware security module (HSM) as a customizable platform to easily integrate into existing software solutions, embed business logic and build s...
One of the biggest challenges when developing connected devices is identifying user value and delivering it through successful user experiences. In his session at Internet of @ThingsExpo, Mike Kuniavsky, Principal Scientist, Innovation Services at PARC, will describe an IoT-specific approach to user experience design that combines approaches from interaction design, industrial design and service design to create experiences that go beyond simple connected gadgets to create lasting, multi-device experiences grounded in people’s real needs and desires.
Samsung VP Jacopo Lenzi, who headed the company's recent SmartThings acquisition under the auspices of Samsung's Open Innovaction Center (OIC), answered a few questions we had about the deal. This interview was in conjunction with our interview with SmartThings CEO Alex Hawkinson. IoT Journal: SmartThings was developed in an open, standards-agnostic platform, and will now be part of Samsung's Open Innovation Center. Can you elaborate on your commitment to keep the platform open? Jacopo Lenzi: Samsung recognizes that true, accelerated innovation cannot be driven from one source, but requires a...
The Internet of Things will greatly expand the opportunities for data collection and new business models driven off of that data. In her session at Internet of @ThingsExpo, Esmeralda Swartz, CMO of MetraTech, will discuss how for this to be effective you not only need to have infrastructure and operational models capable of utilizing this new phenomenon, but increasingly service providers will need to convince a skeptical public to participate. Get ready to show them the money! Speaker Bio: Esmeralda Swartz, CMO of MetraTech, has spent 16 years as a marketing, product management, and busin...
SYS-CON Events announced today that TeleStax, the main sponsor of Mobicents, will exhibit at Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. TeleStax provides Open Source Communications software and services that facilitate the shift from legacy SS7 based IN networks to IP based LTE and IMS networks hosted on private (on-premise), hybrid or public clouds. TeleStax products include Restcomm, JSLEE, SMSC Gateway, USSD Gateway, SS7 Resource Adaptors, SIP Servlets, Rich Multimedia Services, Presence Services/RCS, Diame...
Things are being built upon cloud foundations to transform organizations. This CEO Power Panel at 15th Cloud Expo, moderated by Roger Strukhoff, Cloud Expo and @ThingsExpo conference chair, will address the big issues involving these technologies and, more important, the results they will achieve. How important are public, private, and hybrid cloud to the enterprise? How does one define Big Data? And how is the IoT tying all this together?
We certainly live in interesting technological times. And no more interesting than the current competing IoT standards for connectivity. Various standards bodies, approaches, and ecosystems are vying for mindshare and positioning for a competitive edge. It is clear that when the dust settles, we will have new protocols, evolved protocols, that will change the way we interact with devices and infrastructure. We will also have evolved web protocols, like HTTP/2, that will be changing the very core of our infrastructures. At the same time, we have old approaches made new again like micro-services...
The only place to be June 9-11 is Cloud Expo & @ThingsExpo 2015 East at the Javits Center in New York City. Join us there as delegates from all over the world come to listen to and engage with speakers & sponsors from the leading Cloud Computing, IoT & Big Data companies. Cloud Expo & @ThingsExpo are the leading events covering the booming market of Cloud Computing, IoT & Big Data for the enterprise. Speakers from all over the world will be hand-picked for their ability to explore the economic strategies that utility/cloud computing provides. Whether public, private, or in a hybrid form, clo...
SYS-CON Events announces a new pavilion on the Cloud Expo floor where WebRTC converges with the Internet of Things. Pavilion will showcase WebRTC and the Internet of Things. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devices--computers, smartphones, tablets, and sensors – connected to the Internet by 2020. This number will continue to grow at a rapid pace for the next several decades.
SYS-CON Events announced today that Gridstore™, the leader in software-defined storage (SDS) purpose-built for Windows Servers and Hyper-V, will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Gridstore™ is the leader in software-defined storage purpose built for virtualization that is designed to accelerate applications in virtualized environments. Using its patented Server-Side Virtual Controller™ Technology (SVCT) to eliminate the I/O blender effect and accelerate applications Gridsto...
SYS-CON Events announced today that Red Hat, the world's leading provider of open source solutions, will exhibit at Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Red Hat is the world's leading provider of open source software solutions, using a community-powered approach to reliable and high-performing cloud, Linux, middleware, storage and virtualization technologies. Red Hat also offers award-winning support, training, and consulting services. As the connective hub in a global network of enterprises, partners, a...
As the Internet of Things unfolds, mobile and wearable devices are blurring the line between physical and digital, integrating ever more closely with our interests, our routines, our daily lives. Contextual computing and smart, sensor-equipped spaces bring the potential to walk through a world that recognizes us and responds accordingly. We become continuous transmitters and receivers of data. In his session at Internet of @ThingsExpo, Andrew Bolwell, Director of Innovation for HP’s Printing and Personal Systems Group, will discuss how key attributes of mobile technology – touch input, senso...
The Internet of Things (IoT) is making everything it touches smarter – smart devices, smart cars and smart cities. And lucky us, we’re just beginning to reap the benefits as we work toward a networked society. However, this technology-driven innovation is impacting more than just individuals. The IoT has an environmental impact as well, which brings us to the theme of this month’s #IoTuesday Twitter chat. The ability to remove inefficiencies through connected objects is driving change throughout every sector, including waste management. BigBelly Solar, located just outside of Boston, is trans...
Connected devices and the Internet of Things are getting significant momentum in 2014. In his session at Internet of @ThingsExpo, Jim Hunter, Chief Scientist & Technology Evangelist at Greenwave Systems, will examine three key elements that together will drive mass adoption of the IoT before the end of 2015. The first element is the recent advent of robust open source protocols (like AllJoyn and WebRTC) that facilitate M2M communication. The second is broad availability of flexible, cost-effective storage designed to handle the massive surge in back-end data in a world where timely analytics...
Internet of @ThingsExpo Silicon Valley announced on Thursday its first 12 all-star speakers and sessions for its upcoming event, which will take place November 4-6, 2014, at the Santa Clara Convention Center in California. @ThingsExpo, the first and largest IoT event in the world, debuted at the Javits Center in New York City in June 10-12, 2014 with over 6,000 delegates attending the conference. Among the first 12 announced world class speakers, IBM will present two highly popular IoT sessions, which will take place November 4-6, 2014 at the Santa Clara Convention Center in Santa Clara, Calif...
From a software development perspective IoT is about programming "things," about connecting them with each other or integrating them with existing applications. In his session at @ThingsExpo, Yakov Fain, co-founder of Farata Systems and SuranceBay, will show you how small IoT-enabled devices from multiple manufacturers can be integrated into the workflow of an enterprise application. This is a practical demo of building a framework and components in HTML/Java/Mobile technologies to serve as a platform that can integrate new devices as they become available on the market.