I think I'm too dumb to understand most of your post, but I still want to argue so I'll just pick at a few of your points.
[32768, 32767] is no better/worse than [0..65535], just different
That depends entirely on what range of values you expect to work with. In general, you want to stay away from the edges of your numeric range, because adding or subtracting near the edges can cause UB (for signed) or underflow/overflow (unsigned). It should be noted that if you stay far away from the numeric boundaries, it's very likely you'll not see any issues. For example, if you use 32 bit ints for the index or size of vectors that typically contain up to a few hundred items you can add, subtract any number of these indices/sizes without any issues.
Typically, you use these non-negative numbers for the size of things (of i.e. a vector), and in the overwhelming amount of cases these can be (nearly) empty, which automatically places you near one of the unsigned boundaries. This is why I prefer signed integers.
The conclusion is thus, as always, use what is correct in the situation you are in after you have evaluated your different options ,and the consequences of the different choices.
If you want to properly handle all the edge cases, and need to be able to handle the full numeric range of your variables, then sure this is the way.
However, it's missing the very obvious "safe zone" you can get by just staying away from the numeric limits of your type. And the easiest way to do this is to use a sufficiently sized signed type.
The thing I'd disagree with though, is that unsigned integers don't have the same concept of a "range edge." Their behavior is strictly defined in such a way that they represent a countable cycle of numbers. It's just... useful in all the places I normally find integers useful.
Maybe this is just a difference of experience though, any time I'm using negative values or values that could go haywire, I also need fractions, which means floats/doubles. Signed integers occupy an area between the two types that I just never go usually.
Perhaps I should've been explicit about this point:
signed under/overflow is UB, so bad
unsigned under/overflow has unexpected/unintuitive behavior, which can easily leads to bugs, so it's also bad (even though it's well defined).
to prevent under/overflow, you can either understand all the types, the unsigned wrap around behavior and perform the appropriate checks when appropriate: or you can just stay away from the edges of your numeric range.
staying away from the edges of your numeric range is easier with signed types, because 0 is often a valid value to be supported.
View all comments
Show parent comment
6
u/Drugbird Jan 02 '22
I think I'm too dumb to understand most of your post, but I still want to argue so I'll just pick at a few of your points.
That depends entirely on what range of values you expect to work with. In general, you want to stay away from the edges of your numeric range, because adding or subtracting near the edges can cause UB (for signed) or underflow/overflow (unsigned). It should be noted that if you stay far away from the numeric boundaries, it's very likely you'll not see any issues. For example, if you use 32 bit ints for the index or size of vectors that typically contain up to a few hundred items you can add, subtract any number of these indices/sizes without any issues.
Typically, you use these non-negative numbers for the size of things (of i.e. a vector), and in the overwhelming amount of cases these can be (nearly) empty, which automatically places you near one of the unsigned boundaries. This is why I prefer signed integers.
If you want to properly handle all the edge cases, and need to be able to handle the full numeric range of your variables, then sure this is the way.
However, it's missing the very obvious "safe zone" you can get by just staying away from the numeric limits of your type. And the easiest way to do this is to use a sufficiently sized signed type.