7

This

<cfif len(x)>

Or

<cfif x NEQ "">

Which one is more efficient and readable? Function call vs comparing empty string?

Henry
  • 32,689
  • 19
  • 120
  • 221
  • 1
    *readable* is going to be a matter of opinion. I prefer using `len`. But in fairness, I think NEQ is more intuitive at first glance. I probably would not worry about efficiency, because I cannot imagine the difference would be that noticeable. Just a guess though. – Leigh Jul 28 '11 at 19:55

5 Answers5

8

I'm with Scott on this one:

<cfif len(trim(x))>

When you assign a database element to a variable, that has a "Null" value, or a single space, it will pass just the length test, the trim fixes that.

I only use just that if I'm in my own code and I'm positive that my variables have been declared. Often I'm abroad in another programmers problem however, so my test would look like:

<cfif isdefined('x') AND len(trim(x)) gt 0>

As for readability, it's not the prettiest, but it's the most thorough.

ale
  • 6,369
  • 7
  • 55
  • 65
Dave Ostrander
  • 226
  • 2
  • 5
  • What does `len(x)` do what x is not defined or contains NULL? –  Jul 28 '11 at 20:31
  • @pst, try it. I bet it will throw "variable 'x' not defined". – Henry Jul 28 '11 at 20:36
  • @Dave I'd scope the x in isDefined(), and the last GT 0 is not really needed, but I understand if u don't wanna mix boolean with number comparison op. :) – Henry Jul 28 '11 at 20:37
  • 1
    @Dave - I agree with you, except on the `null` part :) `null` is usually treated as an empty string and len("") returns false. – Leigh Jul 28 '11 at 20:45
  • 1
    at Henry and Leigh, I agree with both of you. The verbose check is just that. If you make it into the inside of that if block, you definitely have a value. The gt 0, I put in for readability. Sometimes the developers that come in behind me don't always pick up on the boolean syntax. – Dave Ostrander Jul 28 '11 at 20:53
  • If you are worried about performance, you need to scope the variable in the isDefined() call or better yet, do a structKeyExists(). Both will be faster than isdefined() with an unscoped variable name. – Scott Stroz Jul 29 '11 at 00:07
4

Personally, I typically use:

len( trim( x ) )
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Scott Stroz
  • 7,510
  • 2
  • 21
  • 25
4

I use:

 if(len("String"));

I think the code is much cleaner and as they like to say "expressive". With this syntax what you are saying with the code is "if this string has a length" do this where with the opposite your code is "saying if the length of the string is a blank string do this".

I also use ColdFusions loose boolean values for lots of things like in addition:

  if(a + b){
   ...
   }
bittersweetryan
  • 3,383
  • 5
  • 28
  • 42
  • 1
    Is it really faster in this case? I find it hard to believe -- would require benchmarks to say for certain. String comparison will fail fast and take `O(min(len1,len2))`, possible quickly aborted if `len1 != len2`. Because of this it will check as most *0 characters* between the two strings, if storing a discreet length, and at most 1 character if using a sentinel terminator. –  Jul 28 '11 at 19:44
  • 2
    I jsut ran some tests and they were within milliseconds of each other through 100,000 iterations of a loop so I'll update my answer. – bittersweetryan Jul 28 '11 at 20:04
2

Ryan's on the money and rightfully has the accepted answer.

The consideration I have in situations like this is that positively-phrased expressions are slightly easier to get one's brain around than negative ones, so as far as clarity goes: using len() is better than a negative comparison to an empty string.

Also from a pragmatic point of you, you're most probably wanting to know if you have a string with length, not that the string happens to not be an empty string (if you see the slight semantic difference), so the len() approach will more closely match your actual requirements.

As for doing a trim(): unless it's coming from user input and it's important that whitespace padding is removed, I would not do this. I am a firm believer in "garbage in, garbage out". It's also second-guessing the intent of the data, and I deeply dislike code that doesn't simply do precisely what it's told, no more, and no less.

There are absolutely no real-world considerations relating to performance here, so don't worry about that sort of thing, instead focus on what makes the most readable code that does the job at hand.

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
0

From what I remember, len(x) or rather len(trim(x)) is faster than x neq ""

Larry C. Lyons
  • 374
  • 1
  • 8
  • 2
    Not (and never) sufficiently so that it should be a consideration. What is *more readable* (which is entirely subjective) is the only real consideration here. – Adam Cameron Aug 20 '13 at 18:23