5 ms·
Not to mention the 1-based indexing sin. JavaScript has a lot of WTFs but they got that right at least.
by IshKebab 11mo ago
Not to mention the 1-based indexing sin. JavaScript has a lot of WTFs but they got that right at least.
- idle_zealot 11mo agoDoes it count as 0-indexing when your 0 is a floating point number?
- krackers 11mo agoActually in JS array indexing is same as property indexing right? So it's actually looking up the string '0', as in arr['0']
- idle_zealot 11mo agoHuh. I always thought that JS objects supported string and number keys separately, like lua. Nope! [Documents]$ cat test.js let testArray = []; testArray[0] = "foo"; testArray["0"] = "bar"; console.log(testArray[0]); console.log(testArray["0"]); [Documents]$ jsc test.js bar bar [Documents]$
- aidenn0 11mo agoThey do, but strings that are numbers will be reinterpreted as numbers. [edit] let testArray = []; testArray[0] = "foo"; testArray["0"] = "bar"; testArray["00"] = "baz"; console.log(testArray[0]); console.log(testArray["0"]); console.log(testArray["00"]);
- minitech 11mo agoThat example only shows the opposite of what it sounds like you’re saying, although you could be getting at a few different true things. Anyway: - Every property access in JavaScript is semantically coerced to a string (or a symbol, as of ES6). All property keys are semantically either strings or symbols. - Property names that are the ToString() of a 31-bit unsigned integer are considered indexes for the purposes of the following two behaviours: - For arrays, indexes are the elements of the array. They’re the properties that can affect its `length` and are acted on by array methods. - Indexes are ordered in numeric order before other properties. Other properties are in creation order. (In some even nicher cases, property order is implementation-defined.) { let a = {}; a['1'] = 5; a['0'] = 6; Object.keys(a) } // ['0', '1'] { let a = {}; a['1'] = 5; a['00'] = 6; Object.keys(a) } // ['1', '00']
- nvlled 11mo agoLua supports even functions and objects as keys: function f1() end function f2() end local m1 = {} local m2 = {} local obj = { [f1] = 1, [f2] = 2, [m1] = 3, [m2] = 4, } print(obj[f1], obj[f2], obj[m1], obj[m2], obj[{}]) Functions as keys is handy when implementing a quick pub/sub.
- chrisweekly 11mo agoExcept for Date.
- coliveira 11mo agoIf you can't deal with off-by-one errors, you're not a programmer.
- nine_k 11mo agoThis indeed is not Algol (or rather C) heritage, but Fortran heritage, not memory offsets but indices in mathematical formulae. This is why R and Julia also have 1-based indexing.
- IshKebab 11mo agoAnd MATLAB. Doesn't make it any better that other languages have the same mistake.
- cmrdporcupine 11mo agoPascal. Modula-2. BASIC. Hell, Logo. Lately, yes, Julia and R. Lots of systems I grew up with were 1-indexed and there's nothing wrong with it. In the context of history, C is the anomaly. I learned the Wirth languages first (and then later did a lot of programming in MOO, a prototype OO 1-indexed scripting language). Because of that early experience I still slip up and make off by 1 errors occasionally w/ 0 indexed languages. (Actually both Modula-2 and Ada aren't strictly 1 indexed since you can redefine the indexing range.) It's funny how orthodoxies grow.
- nine_k 11mo agoPascal, frankly, allowed to index arrays by any enumerable type; you could use Natural (1-based), or could use 0..whatever. Same with Modula-2; writing it, I freely used 0-based indexing when I wanted to interact with hardware where it made sense, and 1-based indexes when I wanted to implement some math formula.
- teo_zero 11mo agoIn fact zero-based has shown some undeniable advantages over one-based. I couldn't explain it better than Dijkstra's famous essay: http://www.cs.utexas.edu/~EWD/ewd08xx/EWD831.PDF http://www.cs.utexas.edu/~EWD/ewd08xx/EWD831.PDF
- cmrdporcupine 11mo agoIt's fine, I can see the advantages. I just think it's a weird level of blindness to act like 1 indexing is some sort of aberration. It's really not. It's actually quite friendly for new or casual programmers, for one.
- bigstrat2003 11mo agoThere's nothing wrong with 1-based indexing. The only reason it seems wrong to you is because you're familiar with 0-based, not because it's inherently worse.
- IshKebab 11mo agoThat's simply untrue. 1-based indexing is inherently worse because it leads to code that is less elegant and harder to understand. And slightly less efficient but that's a minor factor.
- klibertp 11mo agoIf a language has a well-designed collections library (think Smalltalk and derivatives, not C++'s STL), the difference between 1 and 0-based indexing is hard even to notice, much less lead to "less elegant" code. Between Stream and Collection subclasses, the API is rich enough that using indexes is reserved for very low-level operations that you seldom use in non-FFI, non-VM code. What you say is true for languages that don't have collections, real arrays, or vectors, only memory ranges. This is the case of C, but not Fortran, Pascal, or Ada. So yeah, if all you have is a hammer, you'd better use 0-based nails; hopefully, though, we'll allow for non-hammer tools in popular toolboxes sometime this century.
- sltkr 11mo agoI'll refer you to Dijkstra's "Why numbering should start at zero": https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831...