I just ran across the following line of code:
if(someInt <= int.MinValue) { /* ... */ }
I'm wondering if I'm missing some subtlety that makes using the <= comparison better than the more intuitive == comparison?
I just ran across the following line of code:
if(someInt <= int.MinValue) { /* ... */ }
I'm wondering if I'm missing some subtlety that makes using the <= comparison better than the more intuitive == comparison?
It's the minimum possible value of int, doesn't seem like there is anything to miss. Go with the code that reads the most intuitively to you.
If someInt is being decremented by some arbitrary amount, the above code makes more sense.
It's just clearer. someInt should never be < int.MinValue, but if it was you wouldn't want the test to suddenly become false right?
As a rule, when my code contains a boundary test, I always use <, <=, > or >= rather than ==. even if it isn't possible to be outside the boundary. This makes the intent clearer.
Doesn't make sense. It's impossible for someInt to be smaller than int.MinValue, == is sufficient.
Using <= would only be confusing.
I have seen this approach advocated because if something goes wrong (memory corruption, for example), the condition will still trigger. This isn't so useful in your case, but in other cases such as a loop that is counting down to 10 and the memory corruption sets the value to -500, it will still terminate (rather than looping forever, or until it rolls over which could be a long time). I'm not sure I believe this is justified, since it seems to me that programming to handle this sort of error is rarely going to do the right thing.
I would say this is a mistake (or possibly the legacy of something that has been changed, i.e. it was a value other than int.MinValue originally).
<= has a totally different meaning to == and just because in this instance they achieve the same thing I would always write the thing that conveys the clearest intent of the code - otherwise people like you come along later to maintain it and have to post an SO question asking why the hell the original developer did it that way :)
The behavior is identical here, but suppose you later changed int.MinValue to some other small value. Using <= means less to change later.
Also, it could make sense if someInt changes one day to a long.
This is a common approach used in loops - instead of (for example)
if (value == max)
break;
use
if (value >= max)
break;
Although you know that value "should never" be greater than max, what happens if some part of the program logic is wrong, and value is passed in as (max + 12)? The first case will try to loop 4 billion times on a 32-bit system; the second case will exit as soon as it realises you've passed the "max" position.
It is therefore a more robust way of saying the same thing, because it will work when everything is set up correctly, but will also work (or at least, not catastrophically fail) when somebody screws up. Processors can do >= in the same time it takes to do == on intrinsic types, so there's no performance difference.
However, it is superfluous (albeit harmless) to use <= when comparing an Int to Int.MinValue, as it is technically impossible to have a value smaller than that in an Int. Of course, an Int64 could still be less than Int32.MinValue.
So I would imagine it was a "force of habit" from the coder. Or they didn't trust Int.MinValue to return the min value :-)
int.MinValue by default uses the minimum value of type Int32.
Your code is fine. I am not sure about the initialization of the variable someInt. It could be Int16, Int32, or Int64 (which can be smaller than int.MinValue).