When to use || vs. OR in ColdFusion in a <cfif>?

10,071

Solution 1

Double pipe (as well as double ampersand) are supported in ColdFusion since CF8. Since learning that, I always use double pipe/double ampersand instead of OR/AND. The reason why I code with this style is that "OR" is not completely descriptive with regards to the operation being performed. There's bitwise-OR, logical-OR, and logical-OR with short-circuiting.

Bitwise OR: 01 | 10 = 11

Logical OR: buildErrorsOn(form.varA) | buildErrorsOn(form.varB)

Logical OR (short-circuit): isDefined('url.doStuff') || isDefined('url.doStuff')

In just about any language you could use right now (Oracle seeming to be a notable exception), double pipe always means Logical-OR with short-circuiting. It's a precise notation.

Solution 2

I prefer || in CFSCRIPT and OR in CFML.

Solution 3

There may be support in CF9 that brings parity to those keywords, though I couldn't find a reference to indicate that. Really, you are probably better off just using the OR keyword in CFIF, and saving the || for string concatenation in SQL queries.

Solution 4

It looks like || has been supported since CF8. CF8 documentation

I always find "or" more intuitive. (For that matter, I always use "+" for string concatenation in SQL - or is that exclusive to SQL Server?)

Share:
10,071
dmr
Author by

dmr

Updated on June 04, 2022

Comments

  • dmr
    dmr almost 2 years

    When do I use an "OR" vs a || in a ColdFusion cfif statement?

  • Leigh
    Leigh about 14 years
    Yes, for sql "+" is SQL Server specific. If memory serves, Oracle uses "||" (confusingly ;), MySQL CONCAT(), ...
  • Matt Gutting
    Matt Gutting about 14 years
    One would hope (or wish, at least) there could be agreement at least on a simple logical operator :-/
  • Leigh
    Leigh about 14 years
    Though I do prefer || and &&, I have to admit it is a lot more likely one would mistake || for | than it is to mistake the long-hand versions OR and BitOr. At least at first ..
  • Leigh
    Leigh about 14 years
    Haha.. we can wish. But that is about all.