advanced cfif statement

27,935

Solution 1

EDIT: Ok, I understand how to use EQ and all that. I posted this in a bit of a hurry. My question is about the parentheses. Is it syntactically correct to use them this way?

Syntactically, yes. The code's syntax is correct and will not throw syntax errors.

However, it's not necessarily the best way to do it. At the very least you should have linebreaks in there, to make it more readable, like so:

<cfif (not isdefined("URL.room") or URL.room EQ "")
    and (not isdefined("URL.system" or URL.system EQ "")
    and (not isdefined("URL.date") or URL.date EQ "")
    >


And I would be more inclined to write it like this:

<cfif NOT
    (  ( isDefined('Url.Room')   AND Len(Url.Room)   )
    OR ( isDefined('Url.System') AND Len(Url.System) )
    OR ( isDefined('Url.Date')   AND Len(Url.Date)   )
    )>

Because that's much more readable, and makes it more obvious that each row is checking the same thing.


That is assuming I was doing this in a single IF statement, anyway.

If you start getting lots of conditions to check, you might want to consider doing something like this instead:

<cfset FieldList = "Room,System,Date" />
<cfset AllFieldsValid = true />
<cfloop index="Field" list="#FieldList#">
    <cfif NOT ( StructKeyExists(Url,Field) AND Len(Url[Field]) )>
        <cfset AllFieldsValid = false />
        <cfbreak/>
    </cfif>
</cfloop>

<cfif AllFieldsValid>
...

Which might look intimidating at first, but is much easier to maintain - you just add a new item to FieldList (and you may already have a variable which serves that purporse).

Anyway, hopefully all this helps - let me know if any questions on it.

Solution 2

I'd prefer...

<cfparam name="URL.room" default="">
<cfparam name="URL.system" default="">
<cfparam name="URL.date" default="">


<cfif len(URL.room) EQ 0 and len(URL.system) EQ 0 and len(URL.date) EQ 0>
   ...
</cfif>

Or if you're comfortable with mixing non-boolean functions and boolean expression

<cfif len(URL.room) and len(URL.system) and len(URL.date)>
   ...
</cfif>

Solution 3

replace the = with eq

Solution 4

In CFML the comparison operators use characters rather than symbols:

==  EQ   
!=  NEQ  
>   GT    
>=  GTE   
<   LT    
<=  LTE   

Similarly with boolean operators:

!   NOT
&&  AND
||  OR

You can still use the traditional symbols in CFScript mode.

Also worth mentioning that Railo, an alternative CFML engine to Adobe ColdFusion, allows you to use the symbols in tag-based code, if there is no ambiguity with closing tag (e.g. the condition is wrapped in parentheses).

Solution 5

@Henry:

 <cfif len(URL.room) EQ 0 and len(URL.system) EQ 0 and len(URL.date) EQ 0>
    ...
 </cfif>

Shorter:

 <CFIF Len(URL.room) AND Len(URL.system) and Len(URL.date)>

Len() is better than EQ ""

Share:
27,935
Jimmy
Author by

Jimmy

I want to build reliable software that genuinely helps people, and I want to do it in a way that looks beautiful both to users and my fellow developers. I'm a developer with a decade of experience building web apps, primarily in C# and JavaScript. Addicted to refactoring.

Updated on April 14, 2020

Comments

  • Jimmy
    Jimmy about 4 years

    How would I create this statement in CF?

    <cfif (not isdefined("URL.room") or #URL.room# EQ "")
          and (not isdefined("URL.system" or #URL.system# EQ "")
          and (not isdefined("URL.date") or #URL.date# EQ "")>
    

    Obviously the parentheses don't work, but illustrate what I am trying to accomplish. What is the syntax for this?

    EDIT: Ok, I understand how to use EQ and all that. I posted this in a bit of a hurry. My question is about the parentheses. Is it syntactically correct to use them this way?