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.

No comments:

Post a Comment