3 ms·
Zero indexing for arrays/collections generally makes sense. If you had the months in a normal array they would obviously be zero-indexed (unless you deliberatel
by tshaddox 5y ago
Zero indexing for arrays/collections generally makes sense. If you had the months in a normal array they would obviously be zero-indexed (unless you deliberately put a placeholder value in the first position in the array, which would be pretty weird since all the normal ways of getting the first thing in the array would break).
The reason it apparently feels weird to many people to have months be zero indexed is that months are often referred to by their ordinal number, and ordinal numbers obviously start at 1.
- WA 5y agoEven if this is true, days in JS still start at 1. It is inconsistent.
- dmz73 5y agoZero indexing of arrays only makes sense in C because array variable is a pointer to first element of the array. In all languages that have actual array type with length and bounds checking, 1 would be a better way to index arrays most of the time and having specified valid range would be the best way.
- yunohn 5y ago> months are often referred to by their ordinal number What do you mean by “often”? Humans always use ordinal numbers for counting things like months, days, weeks, etc. There is no concept of 0 to be applied here, except for programming array indexing purposes.
- tshaddox 5y agoWhat I mean is that people use the ordinal number as the name of the month (as well as the day of the month). If you ask someone their birthday they’ll often say something like “five twenty, nineteen eighty-four.” This is a potential cause for confusion when writing computer code, because it’s very common for code to refer to items in a collection starting at zero.
- kevin_thibedeau 5y agoIt is lifted from C's struct tm. If you're maintaining tables of month names you're going to 0-index them so tm_mon is 0-based to facilitate that.
- SavantIdiot 5y agoSide note regarding humans: I was in a hotel in France that referred to the ground floor as "floor zero" in their elevator. The only time I've ever seen zero used to count something IRL.
- tshaddox 5y agoThis is actually a decent way to demonstrate why zero-indexing for arrays makes sense. If your friend gives you instructions to their apartment “go to the elevator in the lobby and go up 3 floors,” which button do you press in the elevator?
- yunohn 5y agoI don’t follow? Even in your specific example, it would be +3 to whatever floor the elevator says you’re currently on. Though, usually people refer to the exact floor number per the elevator signs.
- istjohn 5y agoLay people indicate the month with an ordinal all the time since it's how dates are standardized (12/30/2021). People don't often indicate the day of the week with a number, they use "Monday" or "Tuesday". So there's no strongly held convention in people's minds about how to number days like there is for months.