Spacious Formatting For Inline ColdFusion Variable Evaluation

Posted April 30, 2007 at 7:52 AM by Ben Nadel

Tags: ColdFusion

I was just reading Elliott Sprehn's test cases for Ray Camden's Friday Puzzler when I came across something very interesting: at the end of his file, he has his hash signs (#) and his variables on different lines for inline evaluation. I have never seen this before, so naturally I had to test it:

  • <!--- Set up girl object. --->
  • <cfset objGirl = StructNew() />
  • <cfset objGirl.Hair = "Brunette" />
  • <cfset objGirl.Cuteness = 9.5 />
  • <cfset objGirl.Hotness = 7.5 />
  • <cfset objGirl.Athletic = false />
  • <cfset objGirl.Curvey = true />
  •  
  •  
  • <!---
  • Determine general "hotness". This may or may
  • not correlate to the hotness factor.
  • --->
  • Girl Is Hot:
  •  
  • #
  • ListFind( "Brunette,Black", objGirl.Hair ) AND
  • (
  • (
  • objGirl.Athletic AND
  • (objGirl.Hotness GTE 8.5)
  • )
  • OR
  • (
  • objGirl.Curvey AND
  • objGirl.Cuteness GTE 8
  • )
  • )
  • #

Running that (to make sure we don't get any ColdFusion errors), we get:

Girl Is Hot: YES

Notice how spaced out the code is? I am not saying that I would necessarily do it this way, but I have variable evaluations that are very long and highly unreadable. It is nice to know that I can make things a bit more readable by breaking it up by lines. I am just shocked that the hash signs don't have to be directly next to either a variable or an expression. Very cool.

Remember people, 80% of the cost of a project goes into maintenance... make your code readable!



Reader Comments

Apr 30, 2007 at 8:07 AM // reply »
5 Comments

I don't find that very readable at all. Im sure ever situation is different and you are forced to write code like that sometimes, but if it starts getting unreadable, and especially if you are having to do the same code more than once, why not pull the logic out into a udf? even if its on the same page, it would be a ton more readable than that.


Apr 30, 2007 at 8:12 AM // reply »
11,238 Comments

@Ryan,

I agree, so far as pulling repeated code out into a UDF. Yes, there is a rare case where something like this would need to be done. And, while you might not find this readable, I am sure it is more readable than having that all on one line.


Apr 30, 2007 at 10:03 AM // reply »
15 Comments

You guys have missed the point. What I really want to know is did your Weird Science girl materialise? :-)


Apr 30, 2007 at 10:41 AM // reply »
11,238 Comments

Still working out the bugs in the computer program ;)


May 1, 2007 at 5:03 AM // reply »
19 Comments

Yeah it works, for the same reason you can do that kind of thing inside a <cfset> statement.

ColdFusion defines the cfset statement as:

<cfset ((Identifier|String) =)? expression>

Where the left hand side of the statement is optional, and the right hand side is an expression that evaluates to some value. Whitespace between any of the parts is valid, CF doesn't care if there's new lines or spaces before, or after the Identifier/String, expression or assignment operator.

The pound syntax in CF, which can be defined as #expression#, is just the right hand side of the cfset statement. We know this because CF forbids placing the left hand side of the statement inside pound signs. (ex. #foo = 1# is a compiler error).

Thus we can reason that whatever is allowed on the right hand side of a cfset is also allowed inside pound signs.

This has some curious implications as far as syntax goes because you can do something like:

<cfloop collection="#

server

#" item="name">
<cfoutput>#name#</cfoutput>
</cfloop>

We can also reason that:

"2sheep" EQ "#

1
+
1

#sheep"

And all kinds of other fun syntactic craziness. I'd not say this kind of thing is best practice for most cases, but it illustrates what CF sees when you use expressions.


May 1, 2007 at 7:15 AM // reply »
11,238 Comments

Yeah, certainly not best practices, but nice to know what can be done in a pinch.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools