Welcome!

You will be redirected in 30 seconds or close now.

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
The BPM world is going through some evolution or changes where traditional business process management solutions really have nowhere to go in terms of development of the road map. In this demo at 15th Cloud Expo, Kyle Hansen, Director of Professional Services at AgilePoint, shows AgilePoint’s unique approach to dealing with this market circumstance by developing a rapid application composition or development framework.

ARMONK, N.Y., Nov. 20, 2014 /PRNewswire/ --  IBM (NYSE: IBM) today announced that it is bringing a greater level of control, security and flexibility to cloud-based application development and delivery with a single-tenant version of Bluemix, IBM's platform-as-a-service. The new platform enables developers to build ap...

"BSQUARE is in the business of selling software solutions for smart connected devices. It's obvious that IoT has moved from being a technology to being a fundamental part of business, and in the last 18 months people have said let's figure out how to do it and let's put some focus on it, " explained Dave Wagstaff, VP & Chief Architect, at BSQUARE Corporation, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
The major cloud platforms defy a simple, side-by-side analysis. Each of the major IaaS public-cloud platforms offers their own unique strengths and functionality. Options for on-site private cloud are diverse as well, and must be designed and deployed while taking existing legacy architecture and infrastructure into account. Then the reality is that most enterprises are embarking on a hybrid cloud strategy and programs. In this Power Panel at 15th Cloud Expo (http://www.CloudComputingExpo.com), moderated by Ashar Baig, Research Director, Cloud, at Gigaom Research, Nate Gordon, Director of T...
The Internet of Things is not new. Historically, smart businesses have used its basic concept of leveraging data to drive better decision making and have capitalized on those insights to realize additional revenue opportunities. So, what has changed to make the Internet of Things one of the hottest topics in tech? In his session at @ThingsExpo, Chris Gray, Director, Embedded and Internet of Things, discussed the underlying factors that are driving the economics of intelligent systems. Discover how hardware commoditization, the ubiquitous nature of connectivity, and the emergence of Big Data a...
SYS-CON Events announced today that Windstream, a leading provider of advanced network and cloud communications, has been named “Silver Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9–11, 2015, at the Javits Center in New York, NY. Windstream (Nasdaq: WIN), a FORTUNE 500 and S&P 500 company, is a leading provider of advanced network communications, including cloud computing and managed services, to businesses nationwide. The company also offers broadband, phone and digital TV services to consumers primarily in rural areas.
“In the past year we've seen a lot of stabilization of WebRTC. You can now use it in production with a far greater degree of certainty. A lot of the real developments in the past year have been in things like the data channel, which will enable a whole new type of application," explained Peter Dunkley, Technical Director at Acision, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that IDenticard will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. IDenticard™ is the security division of Brady Corp (NYSE: BRC), a $1.5 billion manufacturer of identification products. We have small-company values with the strength and stability of a major corporation. IDenticard offers local sales, support and service to our customers across the United States and Canada. Our partner network encompasses some 300 of the world's leading systems integrators and security s...
DevOps Summit 2015 New York, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that it is now accepting Keynote Proposals. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development cycles that produce software that is obsolete at launch. DevOps may be disruptive, but it is essential.
"People are a lot more knowledgeable about APIs now. There are two types of people who work with APIs - IT people who want to use APIs for something internal and the product managers who want to do something outside APIs for people to connect to them," explained Roberto Medrano, Executive Vice President at SOA Software, in this SYS-CON.tv interview at Cloud Expo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
Nigeria has the largest economy in Africa, at more than US$500 billion, and ranks 23rd in the world. A recent re-evaluation of Nigeria's true economic size doubled the previous estimate, and brought it well ahead of South Africa, which is a member (unlike Nigeria) of the G20 club for political as well as economic reasons. Nigeria's economy can be said to be quite diverse from one point of view, but heavily dependent on oil and gas at the same time. Oil and natural gas account for about 15% of Nigera's overall economy, but traditionally represent more than 90% of the country's exports and as...
The Internet of Things is a misnomer. That implies that everything is on the Internet, and that simply should not be - especially for things that are blurring the line between medical devices that stimulate like a pacemaker and quantified self-sensors like a pedometer or pulse tracker. The mesh of things that we manage must be segmented into zones of trust for sensing data, transmitting data, receiving command and control administrative changes, and peer-to-peer mesh messaging. In his session at @ThingsExpo, Ryan Bagnulo, Solution Architect / Software Engineer at SOA Software, focused on desi...
"At our booth we are showing how to provide trust in the Internet of Things. Trust is where everything starts to become secure and trustworthy. Now with the scaling of the Internet of Things it becomes an interesting question – I've heard numbers from 200 billion devices next year up to a trillion in the next 10 to 15 years," explained Johannes Lintzen, Vice President of Sales at Utimaco, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
"For over 25 years we have been working with a lot of enterprise customers and we have seen how companies create applications. And now that we have moved to cloud computing, mobile, social and the Internet of Things, we see that the market needs a new way of creating applications," stated Jesse Shiah, CEO, President and Co-Founder of AgilePoint Inc., in this SYS-CON.tv interview at 15th Cloud Expo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that Gridstore™, the leader in hyper-converged infrastructure purpose-built to optimize Microsoft workloads, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Gridstore™ is the leader in hyper-converged infrastructure purpose-built for Microsoft workloads and designed to accelerate applications in virtualized environments. Gridstore’s hyper-converged infrastructure is the industry’s first all flash version of HyperConverged Appliances that include both compute and storag...
Today’s enterprise is being driven by disruptive competitive and human capital requirements to provide enterprise application access through not only desktops, but also mobile devices. To retrofit existing programs across all these devices using traditional programming methods is very costly and time consuming – often prohibitively so. In his session at @ThingsExpo, Jesse Shiah, CEO, President, and Co-Founder of AgilePoint Inc., discussed how you can create applications that run on all mobile devices as well as laptops and desktops using a visual drag-and-drop application – and eForms-buildi...
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...
Code Halos - aka "digital fingerprints" - are the key organizing principle to understand a) how dumb things become smart and b) how to monetize this dynamic. In his session at @ThingsExpo, Robert Brown, AVP, Center for the Future of Work at Cognizant Technology Solutions, outlined research, analysis and recommendations from his recently published book on this phenomena on the way leading edge organizations like GE and Disney are unlocking the Internet of Things opportunity and what steps your organization should be taking to position itself for the next platform of digital competition.
The 3rd International Internet of @ThingsExpo, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that its Call for Papers is now open. The Internet of Things (IoT) is the biggest idea since the creation of the Worldwide Web more than 20 years ago.
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 @ThingsExpo, Andrew Bolwell, Director of Innovation for HP's Printing and Personal Systems Group, discussed how key attributes of mobile technology – touch input, sensors, social, and ...