The widespread use of zero-based indexing in programming languages, where the first element of an array is assigned the index 0, may seem counterintuitive to some. This convention, which originated in C, has been adopted by many high-level languages, despite its unusual nature.
The reason for this convention lies in the way C represents arrays as pointers to memory addresses, with the index representing the offset value from the base address. In other words, the absence of any offset represents the first element, which is assigned the index 0.
Later, high-level languages introduced the concept of indexing, which refers to the use of a numeric value to identify the position of an element in an array. However, the influence of C's indexing convention led to the adoption of zero-based indexing in many languages. Only a few languages, such as Lua and MATLAB, use one-based indexing.
This historical convention has led to some inconsistencies in programming. For example, line numbers in code are typically one-based, meaning that the first line of code is assigned the number 1. This contrasts with the zero-based indexing used in many programming languages.
The issue is not unique to programming languages. Game programming languages, such as the mlog language used in Mindustry, also use zero-based indexing for line numbers. This allows for accurate specification of line numbers when using the `jump` instruction.
The result is a mixed bag, with some languages using zero-based indexing and others using one-based indexing. This has led to a request for support for zero-based line numbering in VSCode, but it was closed by Microsoft developers citing a lack of support from other editors.
The influence of history on software design is evident in this example. The conventions adopted in the past continue to shape our programming practices, even when they may seem illogical or inconsistent. As programmers, we must be aware of these conventions and adapt to them, even when they don't align with our intuition.
Source: Bilibili UP 主