I’ve got some free time to kill so I’d like to take a few moments to pass on little tricks or things about Coldfusion that I’ve learned along the way.
Working With LIsts
Using ListAppend
<cfset myList = "one,two,three">
<cfset myList = ListAppend(myList,"somevalue")>
as opposed to
<cfset myList = myList & "somevalue,">
============
Taking full advantage of List delimiters
As you might know, Coldfusion can turn ANY string into a list, simply by defining a delimiter. The comma is the standard delimiter and one that most people use. However, you might now know that you can use the pipe character “|” as a delimiter along with the comma to build a flexible string that allows you to use Coldfusion’s powerful List functions. Here’s some code:
<cfset couples = "andy,jaime|matt,aimee|shawn,leslie">
<cfoutput>
<cfloop index="couple" list="#couples#" delimiters="|">
couple: #couple#<br>
<cfloop index="person" list="#couple#" delimiters=",">
-----person: #person#<br>
</cfloop>
</cfloop>
</cfoutput>
What if you wanted to get a count of all of the words in a string. Some people might do some sort of loop over the string and count the number of spaces but that’s a lot of code. Why not do it in one line like so:
<cfset gettysburg = "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.">
<cfset totalwords = ListLen(gettysburg," ")>
Handy Code Shortcuts
When performing a CFIF statement when working with values that will be either true/false, yes/no, 1/0 etc. you can save a tiny snippet of code each time by stating this:
<cfset amITrue = "yes">
<cfif amITrue>
print me
</cfif>
instead of this
<cfset amITrue = "yes">
<cfif amITrue IS "yes">
print me
</cfif>
This one’s not directly Coldfusion related but it does have to do with coding. If you’re like me and handcode everything then finding yourself a good text editor is essential. Just as important is finding a text editor that offers code completion. My text editor, EditPlus (EP), offers user defined code snippets. Type a word and EP completes the rest of the code for you AND in some cases places the cursor where you’d need to start typing next. Here’s an example.
I type “<cfquery” and then a space and EP gives me this (and places my cursor between the two CFQUERY tags.
<cfquery name="" dataSource="#Application.DSN#" dbType="#Application.DBType#" username="#Application.username#" password="#Application.password#">
</cfquery>
I type “INSERT” and a space and EP gives me this:
INSERT INTO table_name (
var_one,
var_two)
VALUES (
'#var_one#',
'#var_two#')
So you can see that not only does having a text editor with customizable auto complete save time, but it also decreases errors because I’m not having to type those pieces of code each time.
That’s it for right now, more to come later. Feel free to add your own time savers to this post but please make sure to test your code and document how it saves time.