When I’ve used older APIs, for example, the C sockets API on Unix, I always notice that people favor less-than (<) over equal-to (==) when comparing their bad return values.
int result = send(...);
if (result < 0) { perror("..."); }
In the cases I’m referring to, the return code is only ever positive, 0, or -1 (with errno set to the right value). So why not just check for an error using (result == -1) instead of (result < 0)?
I’m asking because I was wondering if it’s done out of habit or if it’s more efficient to use less-than? I was thinking about the fact that if you were comparing two uint64_ts and you found the difference in the MSB, you wouldn’t have to check the other 7 bytes, etc. I might be reaching with this logic though!
I think that this is neither for habit nor for efficiency. It is safer because you don’t rely on a specific return code. Instead, you rely for the error code to be negative. For instance,
strcmpfunction returns “a negative value” when the first string is smaller than the second. On most implementations, it will return-1, but the correct way to handle it is to check for< 0