If you have ever used the iif() (inline if) function in ColdFusion you have probably found yourself using the de() (delay evaluate) function as well. I remember using that function for quite some time without ever really understanding when I needed it and exactly what it did. I found myself explaining it to someone last week and thought it would be an interesting topic to blog.

What does it do?

I'll start by explaining what the function does because it is insidiously simple. I used to think there was some sort of deep black magic baked into the function. What kind of code could be so powerful to reverse the annals of time, disrupt the time-space continuum, and literally delay the inevitable evaluation of my code? I was quite disappointed when I finally read the docs on and found out:
[code] Escapes any double-quotation marks in the parameter and wraps the result in double-quotation marks.[/code]
What- simple string manipulation?? That's all it does? Yep. You could accomplish the same thing with a replace statement and a little concatenation. Take the following code. I'm using cfsavecontent so the double quotes required to create a string in a cfset won't get in the way.
[code]<cfsavecontent variable="my_string">Hello World</cfsavecontent>
<cfoutput>#de(my_string)#</cfoutput>[/code]
This simply outputs:
[code]"Hello World"[/code]
All it did was add double quotes around my string!
What about this?
[code]<cfsavecontent variable="my_string">We're going to use "laser" beams.</cfsavecontent>
<cfoutput>#de(my_string)#</cfoutput>[/code]
Output:
[code]"We're going to use ""laser"" beams." [/code]
Notice the double quote in the string got doubled up. Yep, you could basically use the following code to make your own de() function:
[code]<cfscript>
	function my_de(my_string)
		{
			return '"' & replace(my_string,'"','""','all') & '"';
		}
</cfscript>[/code]
Ok, sorry for the long review of such a simple concept, but I used CF for years and never realized all that. I know-- it's shaming...

Why do I need it?

The iff() function treats the second and third parameters as expressions, which basically means they are going to get evaluated twice before everything is said and done. I racked my brain for a realistic example of why you would actually want to do this, but it's late, so this is all I could come up with:
[code]<cfset favorite_food = "Tacos">
<cfset favorite_color = "Blue">

<cfset option_1 = "food">
<cfset option_2 = "color">

<cfoutput>#iif(1 eq 1,"favorite_" & option_1,"favorite_" & option_2)#</cfoutput>[/code]
The second argument to the function was evaluated twice. The first time around, it equaled favorite_food, which was the equivalent of:
[code]<cfoutput>#favorite_food#</cfoutput>[/code]
The second time around we are simply left with Tacos. Most of the time we aren't using a variable or expression which evaluates to another variable which evaluates to our actual string. We probably just want the contents of a variable:
[code]<cfset favorite_food = "Tacos">
<cfset favorite_color = "Blue">

<cfoutput>#iif(1 eq 1,de(favorite_food),de(favorite_color))#</cfoutput>[/code]
Or better yet, just a string itself:
[code]<cfoutput>#iif(1 eq 2,de("Tacos"),de("Blue"))#</cfoutput>[/code]
Those two examples are the same as:
[code]<cfoutput>#iif(1 eq 1,"favorite_food","favorite_color")#</cfoutput>[/code]
And
[code]<cfoutput>#iif(1 eq 1,"""Tacos""","""Blue""")#</cfoutput>[/code]
Because either way, you ultimately get:
[code]<cfoutput>#favorite_food#</cfoutput>[/code]
And
[code]<cfoutput>#"Tacos"#</cfoutput>[/code]
(Yes, you can put a quoted string in pound signs. They sort of cancel each other out) Man, this is getting confusing. I'd better press on :)

Do I absolutely HAVE to use it?

Nope. Look at my example above. I started with two code samples using de() and then provided two equivalent versions which didn't use it. If you want to manually wrap your string in double quotes and escape (double-up) any quotes in the string feel free to do so. The advantage that de() gives you is cleaner code. Counting quotes can be a downer. Check out this already hairy iff:
[code]<cfoutput>The quote for today is: #iif(dayofweek(now()) eq 6,de("""Thank goodness it's Friday."""),de("""I hate today."""))#</cfoutput>[/code]
Whose output on a Friday is:
[code]The quote for today is: "Thank goodness it's Friday." [/code]
It can be written to do the exact same thing without the de(), but look at all those double quotes!
[code]<cfoutput>The quote for today is: #iif(dayofweek(now()) eq 6,"""""""Thank goodness it's Friday.""""""","""""""I hate today.""""""")#</cfoutput>[/code]
That's 7 quotes.
A single double quote to enclose the string.
Two double quotes to delay its evaluation (escaped once).
Four double quotes for the actual double quote that is part of the string (escaped twice) Yeah, I'm going to bed. I know it gets hairy, but remember, de() is really pretty simple. You don't NEED it, but it can sure clean up at least one round of quotes for you. :)