tags:

views:

146

answers:

4

Is there a way of writing this logic in a single, elegant line of code?

<cfif ThumbnailWidth EQ 0>
   <cfset Width = 75>
<cfelse>
   <cfset Width = ThumbnailWidth>
</cfif>
+2  A: 

Like Neil said, it's fine the way it is. If you really want a single line you could make it a cfscript with a ternary operator, like:

<cfscript>width = (ThumbnailWidth == 0) ? 75 : ThumbnailWidth;</cfscript>

Haven't tested this code, but it should work.

Pablo
This will not work below ColdFusion 9.
Jayson
Good point, not sure why I didn't mention that. Thanks!
Pablo
+14  A: 

Coldfusion 9:

<!--- Syntax: ((condition) ? trueStatement : falseStatement) --->
<cfset width = ((ThumbnailWidth EQ 0) ? 75 : ThumbnailWidth) />

Coldfusion 8 and below:

<!--- Syntax: IIf(condition, trueStatement, falseStatement) --->
<cfset width = IIf((ThumbnailWidth EQ 0), 75, ThumbnailWidth) />

Some will say that IIf() is to be avoided for performance reasons. In this simple case I'm sure you'll find no difference. Ben Nadel's Blog has more discussion on IIF() performance and the new ternary operator in CF 9.

Dan Sorensen
Sergii
Does anyone with CF9 know whether I can use == rather than EQ in the CF9 version? I'm guessing it's still CF best practice to use EQ since it's backwards compatible, but wouldn't matter in this case.
Dan Sorensen
In actual practice, for code maintainability, I'd replace the hard coded 75 with a variable that can be set in a config file.
Dan Sorensen
Yes, you may use ==, but some operators that has '>' or '<' should only be used in CFScript.
Henry
+6  A: 

I find your original elegant enough - tells the story, easy to read. But that's definately a personal preference. Luckily there's always at least nine ways to do anything in CFML.

You can put that on one line (CFML has no end-of-line requirements):

<cfif ThumbnailWidth EQ 0><cfset Width = 75><cfelse><cfset Width = ThumbnailWidth></cfif>

You can also use IIF() Function - it'll do the trick:

<cfset Width = IIf(ThumbnailWidth EQ 0, 75, ThumbnailWidth)>

This construct is a little odd tho' - is more clear I think. The strength of IIF() is that it can also be used inline (it is a function after all). For example:

<img src="#ImageName#" width="#IIf(ThumbnailWidth EQ 0, 75, ThumbnailWidth)#">

This last form is often used to maintain a clean(er) HTML layout while injecting dynamic code.

Hope this helps,

Jim Davis

Jim Davis
I believe that `IIf()` requires the use of `DE()`.
Tomalak
@Tomalak: I don't think it's required in this case since we're passing integers rather than strings. (source: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_c-d_37.html)
Dan Sorensen
Exactly - DE() is "Delay Evaluation" - and it's probably the most confusing part of using IIF(). By default each clause of IIF() is evaluated (as if in the Evaluate() function). If that's NOT what you want you have to use DE() - but in this case these are simple expressions so it doesn't matter. It won't hurt to use it - but it's not needed.
Jim Davis
+1  A: 

I personally prefer something more along the lines of this:

<cfscript>
  Width = ThumbnailWidth;
  if(NOT Val(Width)) // if the Width is zero, reset it to the default width.
    Width = 75;
</cfscript>
Jeff Howden