6

Is checking count/size/length less than 0 necessary in modern languages anymore? For instance, in C# I quote often will check if a collection's count is less than or equal to 0.

I assume that this was done in older languages, like C, where you could potentially have a size of less than 0.

Is there ever a case where you can have the size/length/count of an object or primitive type be less than 0?

TruthOf42
  • 752
  • 1
  • 7
  • 15
  • Are you only asking about `a.Length != 0` versus `a.Length <= 0` (and the equivalent form using `<`)? A slightly different but related question (which you may be getting at with this) is why signed types are used for length and indices, permitting nonsense like negative lengths. –  Jan 09 '14 at 22:03
  • if the collection is designed right, the count/size/length should be `unsigned int`/`uint`, which is impossible to have negative value – Bryan Chen Jan 09 '14 at 22:29
  • @BryanChen: The framework classes are not designed that way, because then they would not be CLS compliant. – Guffa Jan 10 '14 at 00:35
  • 1
    Years ago, defensive coding techniques were all the rage. Programmers were taught to write a.Length <= 0 rather than a.Length != 0, because you can't always trust the computer to return a positive value. I still code a.Length <= 0, because I'm covered if the value is negative. It's the same reason I code a.Length >= maxValue to end a loop. – Gilbert Le Blanc Jan 10 '14 at 13:34
  • Since you mentioned C, personally I think a good C coder will _never_ use a _signed_ integer for sizes, in which case `<= 0` would be by definition equivalent to `== 0`. (And before anyone mentions the signed `ssize_t` or something like it, I'd say that type is semantically not purely size - it's overloaded for reporting errors, and all uses I know of, by the time you're using it as size, you should either assign it into `size_t`, or you already know it's not negative.) – mtraceur Apr 08 '16 at 06:26

2 Answers2

12

In the .NET Framework (of which the C# language is a part), most ordinary indexes (with the notable exception of file positions, which use a 64 bit signed long) are standardized to a signed, 32 bit int.

So, for example, when you get the length of a string, you're being returned a number that can hold negative values, but in practice, will never be negative.

However, that's not true of all string operations. IndexOf() still returns a signed int, but will return a value of -1 if the search string is not found. This is partly why signed int was chosen for such indexes; to provide the possibility of returning negative values for signaling purposes.

Because signed integers are always returned from such operations, the code

if (s.length <= 0)

is consistent with the idea that "I'm checking to see if this string has something in it," since all such lengths will be a positive number.

But you're right; technically it's not necessary to check for a negative number in those cases where you'll never get one (i.e. Length).

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
3

You can use Any which will return as soon as if there is an item in the collection, whereas using Length will actually iterate through the whole collection and count it, which is a bad idea. So Any is lazy, Length is eager. Most of the cases, Any is sufficient.

DarthVader
  • 168
  • 3
  • I know your answer is quite old now, however for other readers I wanted to point something out. You are referring to `Count()` and not `Length`. `Count()` is a Linq function, while `Length` is a property. The property `Length` (or `Count`) will not iterate through the whole collection, but just return a value. So please note the distinction between `Count()` and `Count`/`Length`. – TiltonJH Nov 01 '22 at 13:18