Are you using myVariable NEQ 0?

March 15, 2012 by Mike Henke    2 Comments
Posted under: ColdFusion

I was looking at my code base today and noticed NEQ 0 all over the place. Then I remembered how ColdFusion handles Boolean values, specifically any number is TRUE except 0 which is FALSE.

It I knew the variable was a number, then I could remove the NEQ 0. This was used often in the context of a query like "if recordcount neq 0 then do this". If the variable was a string or list only removing NEQ 0 wouldn't work, I had to add "not isnumeric() or myvariable". Below is the test file to make sure the variables are returning the same boolean.

I am running the NEQ 0 check, then if true, setting a key in a structure, then running a check without NEQ 0. I am doing this check on a several numbers, strings, and lists.

My conclusion is you don't ever need to use NEQ 0 with a variable that is numeric. It is cleaner and less code. For a variable that maybe a string or list when comparing to 0, it maybe easier to do NEQ 0

2 Comments + Add Comment

  • G.Arlington

    Most of the time when one uses
    <cfif var1 NEQ 0>
    it is followed by
    <cfset var2 = var3 / var1 />
    </cfif>

    In this case <cfif var1 NEQ 0> is much clearer for someone reading the code than <cfif var1>

  • Mike Henke

    Either way is readable for me plus the neq I have heard is more "work" for coldfusion. It seems extra, unneed code in this case.

  • Got anything to say? Go ahead and leave a comment!