now musing at www.aliaspooryorik.com!

February 14, 2008

Coldfusion 8 shorthand

Filed under: ColdFusion — aliaspooryorik @ 12:32 pm
Tags:

Coldfusion 8 has a few shortcuts when writing code, these may have appeared in ColdFusion 7, but I’ve jumped from 6.1 to 8 so you’ll have to excuse me if everyone knows this already. Think I’ll stick with long hand for backwards compatibility and for ease of reading for other developers but worth noting.

StructNew()

Before:

<cfset myStruct = StructNew() />
<cfset myStruct.foo1 = "A" />
<cfset myStruct.foo2 = "B" />
<cfset myStruct.foo3 = "C" />

CF8:

<cfset foo = {foo1='A', foo2='B', foo3='C'} />

ArrayNew()

Before:

<cfset foo = ArrayNew(1) /> 

<cfset ArrayAppend(myArray, "A") />
<cfset ArrayAppend(myArray, "B") />
<cfset ArrayAppend(myArray, "C") />

CF8:

<cfset foo = ['A','B','C'] />

Operators

Not

Before:

<cfif Not StructKeyExists(attributes, "myvar")>

CF8:

<cfif ! StructKeyExists(attributes, "myvar")>

IncrementValue

Before:

<cfset foo = foo+1 />

CF8:

<cfset foo++ />

Before:

<cfset foo = foo-1 />

CF8:

<cfset foo-- />

Mod

Before:

<cfif currentrow mod 2>class="alt"</cfif>

CF8:

<cfif currentrow % 2>class="alt"</cfif>

And

Before:

<cfif foo eq 1 And foobar eq 3>Hello World</cfif>

CF8:

<cfif foo eq 1 && foobar eq 3>Hello World</cfif>

OR

Before:

<cfif foo eq 1 OR foobar eq 3>Hello World</cfif>

CF8:

<cfif foo eq 1 || foobar eq 3>Hello World</cfif>

Concatenation

Before:

<cfset foo = "hello" />
<cfset foo = foo & " world" />

CF8:

<cfset foo = "hello" />
<cfset foo &= " world" />

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.