Better HN
topnewbestaskshowjobs
Log inSign up

bringbart

6 karma · joined Oct 10, 2025 (1y ago) · 2 submissions

Submissions

comment 1y ago · link · parent

unsigned is easier: 'if(index >= max)' and has fewer edge cases because you don't need to worry about undefined behavior when computing index.

comment 1y ago · link · parent

>while unsigned wraparound leads to bugs which are basically impossible to find.

What?

unsigned sizes are way easier to check, you just need one invariant:

if(x < capacity) // good to go

Always works, regardless how x is calculated and you never have to worry about undefined behavior when computing x. And the same invariant is used for forward and backward loops - some people bring up i >= 0 as a problem with unsigned, but that's because you should use i < n for backward loops as well, The One True Invariant.