Tuesday, June 15, 2010

Handy CFScript Cheat Sheet

Was looking up the CFScript syntax for Try/Catch again and found this:

CFSCRIPT ColdFusion Cheat Sheet

Thursday, June 10, 2010

Readable string concatenation in CFScript

Once again, (still) working on refactoring large blocks of spaghetti CFML code for all the right reasons. I'm still moving over to CFScript where I'm able as part of the process and I came across a bit of a code readability obstacle.

So, you may know that in JavaScript, you can concatenate a string variable as follows:
var someText = "This is the first part. ";
someText += "And this is the second. ";
alert(someText);
//alert text is "This is the first part. And this is the second. "
This is really a short-hand for:
var someText = "This is the first part. ";
someText = someText + "And this is the second. " ;
alert(someText);
//alert text is "This is the first part. And this is the second. "
Well, as it turns out, += doesn't work on strings in CFScript. So, I got to thinking about how we concatenate string variables with literals in Coldfusion:
var string1 = "This is the first part. ";
var string2 = string1 & "And this is the second. ";
writeOutput(string2);
//results in "This is the first part. And this is the second. "
. . .and that is what led me do test using &= for short-hand concatenation. It works!
 var someText = "This is the first part. ";
someText &= "And this is the second. ";
writeOutput(string2);
//results in "This is the first part. And this is the second. "
This is very valuable to me for two reasons that are really the same reason. First, when I break concatenation up into multiple lines, it's more readable and the logic is clear--especially when there may be one or more variables within the string. Second, the editor I'm using right now is CFEclipse on Eclipse and the color coding and error detection in there seems to get easily confused when long literals mixed with variables are placed in one assignment statement. Here is an example of what I'm writing about:
screen_text = "ERROR Something went wrong. Something went wrong. Something went wrong. Something went wrong. Something went wrong. Something went wrong. Something went wrong. Something went wrong. Something went wrong. #errorBrief# Something went wrong. #errorDetails# ";
With short-hand concatenation, I can make this easier for me, other developers AND text-editors and IDEs to make sense of what's going on here:
screen_text = "ERROR Something went wrong. Something went wrong. ";
screen_text &= "Something went wrong. Something went wrong. Something went wrong. ";
screen_text &= "Something went wrong. Something went wrong. Something went wrong. ";
screen_text &= "Something went wrong. ";
screen_text &= errorBrief; 
screen_text &= " Something went wrong. ";
screen_text &= errorDetails;
Some might suggest that that is no different than "inline" concatenation:
screen_text = "ERROR Something went wrong. Something went wrong. "
  & "Something went wrong. Something went wrong. Something went wrong. "
  & "Something went wrong. Something went wrong. Something went wrong. "
  & "Something went wrong. "
  & errorBrief
  & " Something went wrong. "
  & errorDetails; 
. . .and, on a basic level, it is NO different. I would just say that in a more real-world scenario, short-hand concatenation is more portable. If I need to refactor the concatenation and, say, move different parts of it to different parts of my business logic, the short-hand concatenation will be easier to move around without much, if any, modification.

Tuesday, June 8, 2010

CFLocation in CFScript

I often find myself converting old, long blocks of CFML to CFScript before attempting to refactor, modularize and patternize that code. I do this both to make the code more readable for me and to make the code more portable since much of my refactoring will be done in CFScript anyway. Unfortunately CFScript doesn't support all of the same functions provided for in CFML--in fact, CFScript functions are a very limited set unless you start digging into the Java functions and how to access them in Coldfusion. Yesterday I ran into a need to have CFLocation in my CFScript. It took me a while but I found a really old blog entry with the following solution:
GetPageContext().getResponse().sendRedirect([URL String]);
It works just as I need it to. I haven't yet done the research but I'm assuming this is an example of accessing the Java side of CF. It will be interesting to explore the three functions AND how, apparently, Java supports chaining similar to the jQuery does.

Monday, June 7, 2010

Split Code View in Eclipse


I just learned about a feature in Eclipse that I was missing from my Homesite+ days.

Some background
For me, Eclipse easily replaces Homesite+ for Coldfusion editing because it has good color coding, auto-formatting and intellisense for CF, HTML, CSS, JavaScript, jQuery and MANY other languages.
Sometimes all of the above languages are, unfortunately, all in the same file and Eclipse is just fine and helpful with this--although, sometimes the color coding gets a little confused on what's going on and reminds me to take every opportunity to refactor old, ugly code.
I use the Aptana (non-pro version) plugin for front-end code support (HTML, CSS, JavaScript, jQuery etc.) and CFEclipse for Coldfusion.
When I'm ready to get off my bum and code in a real OO language, Eclipse also supports (and, I believe, originally was made for) Java Development.

OK, get on with it already
Sometimes I edit very long CF files and would like to copy or migrate code from one part of the file to another.
Sometimes the length of the file makes it difficult to do this carefully and accurately. Enter split code editing.

In this video, I right-click the tab for the file I'm editing and select "New Editor." You can also select the tab you want to split-edit and go to the Window menu to select "New Editor"
You can edit and save in either tab.
Each tab tracks changes in the other.
I haven't tested whether each tab tracks "unsaved" changes in the other.
Switching between recent tabs via the keyboard is control+F6 (hold down to select from context menu or quick-release to go to most recent "other" tab.