Enumeration index starting with 0 or 1

2 posts / 0 new
Last post
Enumeration index starting with 0 or 1

Shouldn't the index of an Enumeration (as the internal representation of Enumeration values) rather start with 0 instead of 1, so we would have the integers 0, 1, 2, 3, etc. for enumerating the possible values like in the case of JS array indexes?

Re: Enumeration index starting with 0 or 1?

This is a general issue in Mathematics and Informatics: should we start counting with 1 or with 0? Mathematicians don't agree on this. The Wikipedia article about the natural numbers says:

There is no universal agreement about whether to include zero in the set of natural numbers. Some authors begin the natural numbers with 0, corresponding to the non-negative integers 0, 1, 2, 3, ..., whereas others start with 1, corresponding to the positive integers 1, 2, 3, ...

Likewise, there is no agreement in Informatics. In some programming languages, the first element of an enumeration (the first enumeration literal) corresponds internally to 0 (e.g. in Java), while in others it corresponds to 1 (e.g. in MySQL).

I think it is preferable to start counting with 1 because this corresponds to natural language where we say the "first" element, and not the "zero-th" element. For retrieving the enumeration label, simply write BookCategoryEL.labels[ enumLit-1].