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
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...
Explosive growth in connected devices. Enormous amounts of data for collection and analysis. Critical use of data for split-second decision making and actionable information. All three are factors in making the Internet of Things a reality. Yet, any one factor would have an IT organization pondering its infrastructure strategy. How should your organization enhance its IT framework to enable an Internet of Things implementation? In his session at Internet of @ThingsExpo, James Kirkland, Chief Architect for the Internet of Things and Intelligent Systems at Red Hat, will describe how to revoluti...
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 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...
P2P RTC will impact the landscape of communications, shifting from traditional telephony style communications models to OTT (Over-The-Top) cloud assisted & PaaS (Platform as a Service) communication services. The P2P shift will impact many areas of our lives, from mobile communication, human interactive web services, RTC and telephony infrastructure, user federation, security and privacy implications, business costs, and scalability. In his session at Internet of @ThingsExpo, Robin Raymond, Chief Architect at Hookflash Inc., will walk through the shifting landscape of traditional telephone a...
SYS-CON Events announced today that Matrix.org has been named “Silver Sponsor” of Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Matrix is an ambitious new open standard for open, distributed, real-time communication over IP. It defines a new approach for interoperable Instant Messaging and VoIP based on pragmatic HTTP APIs and WebRTC, and provides open source reference implementations to showcase and bootstrap the new standard. Our focus is on simplicity, security, and supporting the fullest feature set.
BSQUARE is a global leader of embedded software solutions. We enable smart connected systems at the device level and beyond that millions use every day and provide actionable data solutions for the growing Internet of Things (IoT) market. We empower our world-class customers with our products, services and solutions to achieve innovation and success. For more information, visit www.bsquare.com.
How do APIs and IoT relate? The answer is not as simple as merely adding an API on top of a dumb device, but rather about understanding the architectural patterns for implementing an IoT fabric. There are typically two or three trends: Exposing the device to a management framework Exposing that management framework to a business centric logic • Exposing that business layer and data to end users. This last trend is the IoT stack, which involves a new shift in the separation of what stuff happens, where data lives and where the interface lies. For instance, it’s a mix of architectural style...
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...
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.
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...
Connected devices are changing the way we go about our everyday life, from wearables to driverless cars, to smart grids and entire industries revolutionizing business opportunities through smart objects, capable of two-way communication. But what happens when objects are given an IP-address, and we rely on that connection, sometimes with our lives? How do we secure those vast data infrastructures and safe-keep the privacy of sensitive information? This session will outline how each and every connected device can uphold a core root of trust via a unique cryptographic signature – a “bir...
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...
Almost everyone sees the potential of Internet of Things but how can businesses truly unlock that potential. The key will be in the ability to discover business insight in the midst of an ocean of Big Data generated from billions of embedded devices via Systems of Discover. Businesses will also need to ensure that they can sustain that insight by leveraging the cloud for global reach, scale and elasticity.
WebRTC defines no default signaling protocol, causing fragmentation between WebRTC silos. SIP and XMPP provide possibilities, but come with considerable complexity and are not designed for use in a web environment. In his session at Internet of @ThingsExpo, Matthew Hodgson, technical co-founder of the Matrix.org, will discuss how Matrix is a new non-profit Open Source Project that defines both a new HTTP-based standard for VoIP & IM signaling and provides reference implementations.

SUNNYVALE, Calif., Oct. 20, 2014 /PRNewswire/ -- Spansion Inc. (NYSE: CODE), a global leader in embedded systems, today added 96 new products to the Spansion® FM4 Family of flexible microcontrollers (MCUs). Based on the ARM® Cortex®-M4F core, the new MCUs boast a 200 MHz operating frequency and support a diverse set of on-chip peripherals for enhanced human machine interfaces (HMIs) and machine-to-machine (M2M) communications. The rich set of periphera...

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.
The Internet of Things (IoT) is going to require a new way of thinking and of developing software for speed, security and innovation. This requires IT leaders to balance business as usual while anticipating for the next market and technology trends. Cloud provides the right IT asset portfolio to help today’s IT leaders manage the old and prepare for the new. Today the cloud conversation is evolving from private and public to hybrid. This session will provide use cases and insights to reinforce the value of the network in helping organizations to maximize their company’s cloud experience.
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...
SYS-CON Events announced today that Matrix.org has been named “Silver Sponsor” of Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Matrix is an ambitious new open standard for open, distributed, real-time communication over IP. It defines a new approach for interoperable Instant Messaging and VoIP based on pragmatic HTTP APIs and WebRTC, and provides open source reference implementations to showcase and bootstrap the new standard. Our focus is on simplicity, security, and supporting the fullest feature set.