I once designed/implemented a language where the biggest mistake I made was using 1-based indexing. At first it looked like it was going to be easier to understand/use but 0-based indexing is actually much more convenient when dealing with indexes arithmetic.
You do realize that there are approximately just as many use cases where 1 based indexing is more natural and involves less operations, right? It’s highly problem and context dependant.
But I’m not trying it make the point that 0 based or 1 based is better or worse than the other. I’m just saying that it’s a borderline immaterial difference for most use-cases and Julia gives many many tools for getting around any problems that may arise when a certain indexing scheme is awkward.
The 0 or 1 based debate is one of the most boring and pedantic arguments one can have and I do my best to ridicule people when they try to start it.
Let’s not ignore the giant elephant in the room: 1-based indexing. I don’t particularly care since I use R and Python but Java, C, C/C++, C# all used 0-based indexing. It’s truly a bizarre choice Julia made there.
I had been comfortably programming in C and python for years and found 0-based indexing to be perfectly sensible. For the last couple of years I've been programming a lot in Julia. Initially I was horrified by the idea of indexing from 1 but I got the hang of it remarkably fast. Now I feel it's totally natural either way, depending what language I'm in.
I think it's only ever been a matter of preference as you say. And Julia was otherwise such a pleasure to write code in that it was easy for me to pay the small price to learn that.
It is a pain, yes, after decades of 0-indexed languages! I switched to mostly-Julia from mostly-C in the last year, and the 1-indexing is the main irritating thing. I have to learn different ways of doing things, which I guess I haven't yet, so the parts in my programs dealing with 1-indexing seem cumbersome and fiddly, every time. 0-indexing just seems mathematically much simpler. And I like things making things as simple as possible. Maybe as I learn different habits, that impression will go away.
It's also about the only thing I really don't like about Julia – so I can understand someone saying 1-indexing is a deal-breaker.
One of the most mindboggling thing about the recurrent 0- vs 1-based indexing discussion is how incredibly rarely that difference is ever used programmatically in Julia. Most of the large julia packages are programmed in a way that doesn't care whether the array is 0 or 1 based. It is important in some other languages, and then I just think people are happy that this is something that everybody can agree to disagree on. I don't think the discussion is very productive though.
Complaining about 1-based indexing in Julia is so... 4 years old! Just use OffsetArrays, and you can use 0 based, or whatever base floats your boat (start underwater with -5, for example!)
Math/Science focused languages have usually used 1-based indexing, historically. Fortran, MATLAB, R, etc. In the primary fields where Julia is expected to be used, 0-based indexing would be a differentiator, not the other way around.
For what it's worth, I've ran into this a couple of times. Maybe it's just because I'm more familiar with 0-based indexing, but I think it's not just that. In every case the 0-based indexing had fewer (+1)'s and (-1)'s than the 1-based code. For instance k%n returns a number in the range [0,n), or in Julia notation [0:n-1]. When you want to do array indexing with that, you need (k%n)+1 in Julia.
Btw, Matlab also uses 1-based indexing, no? I thought that Julia chose 1-based indexing to get brain backwards compatibility with Matlab.
Actually, it is quite easy to implement 0 based indices or any other indexing scheme in julia, since all the array indexing code is implemented in julia itself.
I personally would find multiple indexing schemes confusing both for usage as well as to develop and maintain. Given that 1 based indexing seems to be a popular choice among many similar languages, we just went ahead with that.
> all serious numerical languages have that. it's more natural
And almost all serious general-purpose languages use 0-based. It is more natural at least to me. You see, this is exactly why 1-based index of Julia is disliked by many.
I see 1-indexing as an artifact of a lot of linear algebra texts, whereas 0-indexing is more popular in analysis. Julia just wants it to be easier to directly copy formulas from publications.
I also think it’s blurry as to whether mathematicians and programmers see indexing numbers as merely indices or richer numbers.
To tell you the truth, I don’t really care about 0 or 1 based indexing, there’s not much difference when I’m coding.
At the same time if you look at TIOBE, 0 based indexing won.
The biggest problem for me was porting numerical code from Ruby: the syntax is so similar to Julia, that I didn’t have much to do, but porting 0 based code to 1 based was the biggest work actually.
I’m sure it’s painful to port code from R or Marlab if it’s 0 based, but that will need to happen anyways.
1 based indexing is very common in many math and statistics oriented languages and frameworks. Since that's the primary target audience for Julia, it makes sense they'd stick with it.
Djikstra argued in favour of 0-based indexing and against 1-based indexing. Let’s just agree that if you’re arguing for 1-based indexing, you’d better have a pretty fantastic argument.
I think for FORTRAN or Julia, using 1-based indexing is a good idea, because most mathematical algorithms are written in notation using 1-based indices. As someone who spent a semester debugging subtle off-by-one errors and small differences in results in his scientific computing classes because I foolishly opted for C rather than FORTRAN, understand that converting algorithms with nested summations and integrators from one convention to another is painful.
Lua should never have used 1-based indexing because it was meant to interface with C code which itself will have data structures that use 0-based indexing. Writing lua extensions for wireshark, for example, is incredibly painful just because of this.
It shouldn't. One-based indexing is what all the research uses, so it is undeniably the better choice for Julia. But zero-based indexing isn't just a memory trick, it makes lots of math easier when you're concatenating ranges or slicing arrays or doing modular arithmetic anywhere near an array.
Everyone in this thread seems to believe that 1-based indices is a non-problem because Julia "offers a choice".
My reaction:
- the default is 1-based, which means the bulk of Julia code will adopt the convention and therefore the vast majority of coders in 2018 will have to do mental gymnastics to understand what the code does.
- when I read Julia code, I will never know which convention the code was written with unless I dig to find where that particular flag is set.
- passing 0-based arrays to a library routine that expects 1-based stuff, what happens then?
- for code that will be 0-based and uses library code that expects 1-based (assuming that's possible without paying copying overhead) will force the code to mentally switch between the two modes. Ugh.
In short: offering is a choice is maybe even worse than enforcing 1-based.
I once designed/implemented a language where the biggest mistake I made was using 1-based indexing. At first it looked like it was going to be easier to understand/use but 0-based indexing is actually much more convenient when dealing with indexes arithmetic.
PS: the "metaprogramming" stuff is fabulous.
reply