News for hackers. Stuff that matters.
Re:Controversial Programming Opinions (2012) by leetcrew on Friday December 04, 2020 @03:47PM (#25307697)
I pretty much agree with all of these but two.

> 1-based indexing is superior to 0-based indexing.

this breaks the identity `arr[idx] == *(arr + idx)`. as a c++ (and sometimes c) dev, I tend to consider "index" and "offset" to be functionally equivalent. hard pass.

> If you use spaces to indent code, or in any way rely on a fixed width font to "line things up", you have a fundamental misunderstanding.

I find "lining things up" to be pretty helpful for spotting mistakes in tedious code (eg, matrix initialization) where you have a bunch of similar lines of code in a row. am I missing your point, or do you just not agree this is useful?

Re:Controversial Programming Opinions (2012) by ghaberek on Friday December 04, 2020 @04:14PM (#25308075)
> this breaks the identity `arr[idx] == (arr + idx)`. as a c++ (and sometimes c) dev, I tend to consider "index" and "offset" to be functionally equivalent. hard pass.

And I think that's the problem. Programmers have collectively conflated the concept of "offset from the address" and "position in the list" as the same concept, and they're not the same thing. Both concepts have their uses but they're separate distinct concepts. C and C++ don't index* a list, they simply offset it, and too many other languages that don't care about in-memory addresses or offsets have carried the concept from offset over to index erroneously.

Re:Controversial Programming Opinions (2012) by leetcrew on Friday December 04, 2020 @04:46PM (#25308535)
I can see how in a higher-level language it might make sense to use 1-based indices. this is, after all, how humans typically count. I just don't agree with it as a general statement over all programming languages. in low-level contexts where you are working directly with memory, it is quite natural for indices (or idx * element_size) to be equivalent to offsets.
Re:Controversial Programming Opinions (2012) by kaba0 on Monday December 07, 2020 @02:39PM (#25336225)
Depending on the context of the program, both can be more comfortable. Pointers/enumerators? 0-based Heavy math problem? 1-based

I believe there is a few languages which can let you set it

Re:Controversial Programming Opinions (2012) by Jenz on Friday December 04, 2020 @05:30PM (#25309161)
> this breaks the identity `arr[idx] == * (arr + idx)`.

Well, then, make the identity `arr[idx] == * (arr + idx-1)`.

Re:Controversial Programming Opinions (2012) by AnimalMuppet on Friday December 04, 2020 @05:43PM (#25309301)
Right. But while I'm just trying to do my work as a programmer, which of those two identities is going to be easier to work with, reason about, and come out of it with correct code? Hint: It doesn't have a "1" in it.

Your identity perfectly illustrates why 1-based arrays are worse rather than better.

Re:Controversial Programming Opinions (2012) by lolinder on Friday December 04, 2020 @11:13PM (#25311682)
That's true for C/C++, but in most languages used today where we don't use pointers, where is the advantage?

I'm hundreds of times more likely to have to write lastElement = array[array.length - 1] (there's the 1 again!) than I am to have to reason about the equivalent pointer arithmetic.

Re:Controversial Programming Opinions (2012) by unishark on Saturday December 05, 2020 @01:43AM (#25312390)
Even in a language without pointers, in a multidimensional array you can index the data one-dimensionally by array[index0 + N * index1] with 0-based indexing, as opposed to array[index0 + N * (index1-1)]. Where N is the length of the inner nested array.

And for that matter all algorithms for which zero-based indexing makes more sense are simpler, such as working with polynomials and expansions where the zeroth power is included in the expansion.

Having said that, I too prefer 1-based indexing because the list of algorithms that are written more cleanly (i.e. without constantly causing bugs when you implement them) is far larger for me personally, including all of linear algebra and related numerical methods.

Re:Controversial Programming Opinions (2012) by AnimalMuppet on Saturday December 05, 2020 @05:34PM (#25319069)
In a language that has one-based indexing, can you treat a multidimensional array as one-dimensional? Or will it give you an array index out of bounds error?

That is, are there any languages that are both one-based, and will let you do that trick?

Re:Controversial Programming Opinions (2012) by unishark on Sunday December 06, 2020 @01:48AM (#25321676)
You would need it to support one-dimensional views (unless your data is just stored as one-dimensional to begin with). Certainly matlab and Julia do. Don't know of any new 1-based languages either way.
Re:Controversial Programming Opinions (2012) by solveit on Saturday December 05, 2020 @01:32AM (#25312330)
I thought we settled on lastElement = array[-1] for this problem (a little, but not entirely, tongue-in-cheek).