4 ms·
> ... 1-based strings and arrays, and you'll see that programs and string expressions are generally shorter and more idiomatic. Depends very much on what you'r
by asveikau 10y ago
> ... 1-based strings and arrays, and you'll see that programs and string expressions are generally shorter and more idiomatic.
Depends very much on what you're doing. If you're doing, say, a filesystem driver, and you need to do math or reasoning on [offset, length] pairs quite frequently, you will find 0-based indexing much more natural. At least that's been my experience. I think some of this applies to strings and memory too.
- coreyd 10y agoThe way I tried to resolve 0 vs 1 based indexing in my head was is they have slightly different contexts. 0-based indexing tells you have far you are from the origin, the first element whereas 1-based indexing is useful for when thinking about how many elements you have.
- deleted 10y ago[deleted]
- dTal 10y agoAh, counting vs measuring! That makes a lot of sense. In the west, we measure our age and start from 0. In China, they count their age (number of distinct years someone has been alive in) and thus start from 1. In fact, in China, if you are born on new years eve you can be 2 years old at 2 days old!