5 ms·
In my opinion, the interactions between unsigned integers and signed integers are too hard for a programmer like myself to handle correctly in practice, especia
by nayuki 7y ago
In my opinion, the interactions between unsigned integers and signed integers are too hard for a programmer like myself to handle correctly in practice, especially being consistently vigilant all the time.
I admit that I didn't understand how the code in the article worked (i.e. why the unsigned check was correct and why the signed check was incorrect) until I read the answer in this comment thread.
I like Java's design where all integer types (except char) are signed, and all arithmetic is done in signed mode. It simplifies the mental model and reduces the opportunities for logic errors.
Signed integers are definitely useful sometimes (e.g. difference of two unsigned indexes). A wide-enough signed type (e.g. int32) can hold all values of a narrower unsigned type (e.g. uint16). So it is possible to design a language without unsigned integer types without losing functionality.
- deleted 7y ago[deleted]
- marcosdumay 7y agoMy current position is that a high level language should have either explicit numeric conversions with very strong typing (like Haskell), or a single integral type (like Python). Anything else is unecesary complexity that won't even bring any sizeable gains. Strong types with implicit conversions (like SQL) isn't too bad, but Java's system is. But C isn't a high level language, so none of the above applies.
- saagarjha 7y ago> C isn't a high level language Depends on your viewpoint, of course :)
- w0mbat 7y agoI agree. This is the reason the Google C++ style guide bans use of "unsigned" wherever it can be avoided. The extra bit of headroom is not worth it, and it's clearer to explicitly check for arguments that are out of range.