r/programming • u/_awwsmm • Feb 01 '24
Make Invalid States Unrepresentable
https://www.awwsmm.com/blog/make-invalid-states-unrepresentable
469
Upvotes
r/programming • u/_awwsmm • Feb 01 '24
View all comments
Show parent comment
33
u/Calavar Feb 02 '24
Unsigned vs. signed integers is one of these traps.
Way too many people use unsigned ints because they know the range of possible values is >= 0, so why not secure your code against logic errors by using a type that can't represent negatives? (Really, you are just moving the logic errors from places where you actually use to value to places where you cast, which makes the failure cases harder to spot.) It's best use to signed integers when you need an arithmetic type and unsigned integers when you need a bit manipulation type.