0

Would you like to code this way?

ColdFusion

Recent blog postings by Jason Delmore, the CFInsider, on ideas for ColdFusion 9 got me thinking about how I code. Taking a look at the beautiful/hacky/cool code I produce I noticed a few things where the language could really help me. The first is I have a large chunks of cfsets and secondly control tags (classified as Flow-control tags on Livedocs) could be simplified.

I find that big chunks of cfset's really clutter up the page and take my eye away from the important part -- what is actually being set. What if there existed a hybrid between cfset and cfscript that allowed the following to happen:

<cfset variables.firstname = "Sam";
	variables.lastname = "Farmer";
	variables.myObject = createObject("component","weather").init();
	variables.temp = variables.myObject.getTemperature("20001");>

(And, yes, I know I could use cfscript but in general I don't like using it as its not feature complete and when I did used to use it in places like above I found it hard to know when to and when not to. Just setting one variable hardly seems worth it, two variables? borderline, then there's the case where I already have two cfsets and now I want to add two more and its "is it worth converting to cfscript?", etc.)

I like the tag and attribute concept (and really how could you like ColdFusion and not?) but in examining my code I kept coming back to the idea that control tags should work differently. This would make them easier to find, quicker to code and more similar to other languages.

For instance looping over an array could use the same syntax as in Javascript:

<cfset variables.arDays = ["Monday","Tuesday","Wednesday","Thursday","Friday"]>

<cfloop variables.i in variables.arDays>
	<cfoutput>#variables.i#</cfoutput>
</cfloop>

Structures, files and lists could be looped over in the same way. A query could be looped over by just saying cfloop getQueryName. I think these concepts could be expanded to other tags (cfswitch immeadiately comes to mind) and would make the switch to CF easier for many folks.

Would you like the code in theses ways? I'm mainly thinking out loud so I'd love to hear your thoughts.

tags:
ColdFusion
Danilo Celic said:
 
It is considered bad practice to loop over arrays using the for-in loop in JavaScript because the for-in loops over the properties *and* exposed methods of an object, and in the case of an array that includes the elements within the array, as well as any methods added to either that particular instance of an array, or to the Array object itself. With the large number of JavaScript libraries that have helper functions for arrays, you run a high risk of getting inaccurate results using that type of looping. This can be observed in the following code:
var days = ["Monday","Tuesday","Wednesday","Thursday","Friday"];

days.indexOf = function(el){
var idx = -1;
// code to run here
return idx;
}

for(var i in days){
alert(days[i]);
}

But this will work with a for=in:
var days = { "Monday":true,"Tuesday":true,"Wednesday":true,"Thursday:true,"Friday":true}

for(var i in days){
alert(days[i]);
}


It is much safer and always accurate to loop over an array using the index of elements from one end to the other. Could be 0 to length-1, or from length-1 to 0, either way will work, depending on the specific needs of your loop.. But the for-in loop, while it may work for you most times, you do run the risk of breaking your program because of the specific way that it works.

Now for ColdFusion, if the variable to iterate over was a structure, then I can can see how the for-in construct that you are suggesting would be good in addition to the following existing loop type:

#variables.arDays[i]#
 
posted 786 days ago
Add Comment Reply to: this comment OR this thread
 
 
@Danilo: Yeah, I've actually been caught by that feature of Javascript loops (your explanation is very good btw), however, that problem would not occur in CF due to the ownership of the language by Adobe.
 
posted 775 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Twitter
You should follow me on Twitter here
About Me
I am a 34-year old Web Developer specializing in ColdFusion. I live and work in downtown Washington, DC with my wife and two daughters. Read more About Me

2007 CFeMmy Best Newcommer winner
As voted on by fellow CF Bloggers.
CFinNC - Carolina ColdFusion / Flex / Air Conference - Oct 17-18, 2009