Why you're wrong about 0-based indexing

  Рет қаралды 28,273

TJ DeVries

TJ DeVries

Күн бұрын

Пікірлер: 695
@alexk.7064
@alexk.7064 4 ай бұрын
Some counterpoints: 0. I have built the habit already. 1. People will judge
@AdolfRizzler41
@AdolfRizzler41 4 ай бұрын
I will judge
@TomasSandven
@TomasSandven 4 ай бұрын
😞
@thepatzer
@thepatzer 4 ай бұрын
I think you mean: 1. I have built the habit already. 2. Segmentation fault (core dumped)
@ssshenkie
@ssshenkie 4 ай бұрын
Here is are some counter counter points: 1. Lua exists 2. People widely use Lua
@diadetediotedio6918
@diadetediotedio6918 4 ай бұрын
@@ssshenkie 0. Lua is a scripting language that don't have all the restrictions and problems other languages have + Lua is the one standing behind the crow not the most used language in the world
@Saeppel
@Saeppel 4 ай бұрын
In Germany, you use the thumb for 1 and not the index finger. So indexing should start from 2 when your program runs on a German server.
@victos-vertex
@victos-vertex 4 ай бұрын
Ackchyually!!!!! In Germany this depends on the context. If you learn counting with your children then you would use your thumb for 1. However, if it's loud and you're asked by another person how many items (like beers) you want, you wouldn't use your thumb to indicate 1, you would use your index finger. So indices should start based on the context the program is used in!
@RenderingUser
@RenderingUser 4 ай бұрын
Wait. There's actually people that _don't_ start counting from their thumb? Edit 5:54 WTF. No nobody does that. Man Americans are weird.
@skaruts
@skaruts 4 ай бұрын
Was the index-finger argument supposed to be taken seriously? Maybe it was humor I didn't get? I hope so. Because it was otherwise an abysmal leap of logic.
@RootsterAnon
@RootsterAnon 4 ай бұрын
@@victos-vertex this is interesting. it would be nice to mark start index in some context to be 0 or 1 or any other for that matter.
@theshooter89
@theshooter89 4 ай бұрын
Some _Inglorious Bastards_ lost his life because of that
@EugenioCanoManuelMendoza
@EugenioCanoManuelMendoza 4 ай бұрын
"Big Industrial C Complex" 🤣
@asagiai4965
@asagiai4965 4 ай бұрын
Technically it is not C which started this.
@JustMog
@JustMog 4 ай бұрын
as a lua enjoyer i can tell you that it's all fun and games until u need to modulo the index
@voidreact
@voidreact 4 ай бұрын
why
@drdca8263
@drdca8263 4 ай бұрын
@@voidreactmodulo n gives you results from 0 to n-1 (for integers. For real numbers the range is [0,n) ) This fits way better for 0-based indexing than for 1-based. for 1-based you would have to say a[((i-1)%n)+1] which is ugly, as opposed to a[i%n] , which is clean.
@groff8657
@groff8657 4 ай бұрын
@@voidreact I think it's because a number modulo the same number results in zero (2 mod 2 = 0). If you're in a situation where you need to cycle back to the start index, such as repeatedly counting 1,2,3... 1,2,3... 1,2,3. In the many other programming languages you need a modulus operation. But due to how the modulus operation works it won't count from '1' but from zero. So 0,1,2... 0,1,2... So you have to account for that "off by one" error.
@maxdehoyos9920
@maxdehoyos9920 4 ай бұрын
Crazy concept I don’t get it :/ why would you have to modulo the index? Is numeric for ‘for i=1,#t’ … not applicable?
@groff8657
@groff8657 4 ай бұрын
​@@maxdehoyos9920 You can do the same thing with numeric for. But there are applications of modulus where it is the easier option. If you're trying to encrypt some "data" with a "key". Let's just say for example using the Vigenere cipher. Since the string is usually longer than the key you use modulus to cycle back to the start index of the key. data = "the quick brown fox jumps over the lazy dog". key = "sec retke ysecr etk eysec retk eys ecre tke". Instead of creating a key which is the same length as the plain-text string. You can just use the modulus operator to cycle back. Another one is when you want to implement your own "shift an array by N steps". let arr = [1,2,3,4,5]; function shiftArr(arr, num) { len len = arr.legnth; let res = []; for (let i = 0; i < len; i++) { let idx = (i+num) % len; // modulus here res[idx] = arr[i]; } return res; } shiftArr(arr, 3); // output [3,4,5,1,2]
@VeitLehmann
@VeitLehmann 4 ай бұрын
There are 2 hard problems in computer science: - Cache invalidation - Naming things - Off-by-1 errors
@henrikholst7490
@henrikholst7490 4 ай бұрын
You mean off by 0 errors?
@skaruts
@skaruts 4 ай бұрын
There are 3 elements in that list, not 2. It doesn't matter if the first element is at index 0 or 1, the element count is still 3. You're conflating counting and indexing, which is wrong. They're not the same thing and that distinction is important.
@Henry-sv3wv
@Henry-sv3wv 4 ай бұрын
@@skaruts there are 10 people that understand this joke. those that do and those that don't
@skaruts
@skaruts 4 ай бұрын
@@Henry-sv3wv the OP wasn't counting in binary. You didn't get his joke.
@rafal06
@rafal06 Ай бұрын
I feel like I'll be making off-by-one errors until the day after I die 💀
@ethanfung1413
@ethanfung1413 4 ай бұрын
Lets all take a step back to appreciate that "the first item should be item 1" is not an argument had in any other discipline
@teej_dv
@teej_dv 4 ай бұрын
Hahaha 😂 this is programmer gold
@drdca8263
@drdca8263 4 ай бұрын
No, people also sometimes argue about this in math. There are a handful of cases where 1-based indexing makes more sense, but for the most part, starting at 0, I.e. starting at the start, makes the most sense. Also, music theory would make a lot more sense if they had used 0-based indexing from the start. Starting at 1 is like, jumping the gun? 1 is the first place you go to, not where you start. You start at the starting line, and then you take a step. You do not start the race having already taken a step past the starting line.
@CarnivorousSpork
@CarnivorousSpork 4 ай бұрын
@@drdca8263 the starting line is my 1st position at the race, which starts at time 0
@drdca8263
@drdca8263 4 ай бұрын
@@CarnivorousSpork not just at time 0, but also position 0. 0 distance from the starting line.
@jsonkody
@jsonkody 4 ай бұрын
xD
@CrazyLuke11
@CrazyLuke11 4 ай бұрын
This teached me something very important: We should make book for children that starts counting at zero.
@catto-from-heaven
@catto-from-heaven 4 ай бұрын
Indexing an array and counting its items are not the same thing
@ollllj
@ollllj 3 ай бұрын
yes
@hamhamofc
@hamhamofc 4 ай бұрын
you have opened my eyes tj, every get_length function should automatically return length-1, the problem was right it front of our eyes how couldve we missed it
@teej_dv
@teej_dv 4 ай бұрын
HAHAHA THIS IS THE BEST ONE YET
@paulosantana9607
@paulosantana9607 4 ай бұрын
This is cursed in so many levels
@soopyboo32
@soopyboo32 4 ай бұрын
array.maxIndex ftw
@JasonDoege
@JasonDoege 4 ай бұрын
last_index is a useful function.
@Turalcar
@Turalcar 4 ай бұрын
The actual answer is that whenever you use ranges, the end is excluded by default
@cryptonative
@cryptonative 4 ай бұрын
There are 1 kinds of people in the world: - Those who use 0-indexing - Those who don’t
@howdoiexitvim-sg2xl
@howdoiexitvim-sg2xl 4 ай бұрын
Two kinds, but their indices are 0 and 1
@paulosantana9607
@paulosantana9607 4 ай бұрын
@@howdoiexitvim-sg2xl ackshually he is using cardinal numbers and implying Those who don't use 0 based index are not people
@cryptonative
@cryptonative 4 ай бұрын
@@paulosantana9607 I'm not, that's why I wrote "1 kinds"
@paulosantana9607
@paulosantana9607 4 ай бұрын
@@cryptonative I know, that's why I wrote "ackshually"
@u9vata
@u9vata 4 ай бұрын
Would have been much better this way: There are 1 kinds of people in the world: - Those who do 1-based indexing - Those who do 0-based indexing ^^I hope you understand why this version is better if you try to parse it with both style indexings in "your logic" - but the real issue is that there would be TWO kinds: but their indices would be 0 and 1 of course - the count does not work zero-based, just the indexing! 🙂
@Quanalogy
@Quanalogy 4 ай бұрын
So the explanation of 0-index meaning in C really made me think 0 based indexing makes even more sense now
@jack.klimov
@jack.klimov 4 ай бұрын
bro, you definitely have steel balls to start this conversation)
@Kane0123
@Kane0123 4 ай бұрын
Immediate memories of the duke nukem sound boards. I’ve got balls of steel. Steel. Steel. Balls. Balls. Balls.
@andygarfield6529
@andygarfield6529 4 ай бұрын
It’s almost like he actually believes what he’s saying.
@ollllj
@ollllj 3 ай бұрын
1-indexing is ignorant trolling (to a point that it is save to assert, they can only comprehend whole numbers from 1 to 128), because: Trigonometry and complex-numbers are 0+0*i centric. (relevant for precision and performance) modulo arithmetic is 0-indexed (strides/matrices, hashes, cryptography) logarithms are 0-indexed (equal temperament of music as one common application) in many languages, the name of weekdays are 0-indexed. twos-complement is 0-indexed. Many languages (like mono-script) have the most efficient loop be a countdown-loop, that has a number as condition (not casting to bool makes it faster) , that is only exit-condition-false when the condition-value reaches 0. 1-indexing is ridiculously low performance and lower precision for anything with complete-numbers or trigonometry or loops.
@hallo7053
@hallo7053 2 ай бұрын
@@ollllj I don't understand how it is lower performance? In lua e.g. they just do always - 1 for sets and gets which is one cpu cycle, if you write a dynamic language (for which only this concept makes sense in first place) then you can't really complain about loosing ONE cpu cycle
@filiflo
@filiflo 4 ай бұрын
I personally find starting with 1 as an index more intuitive. However starting from an index of 1 just throws away a perfectly fine number to index from. Starting from 1 reduced a byte index from 256 possible indices to 255. While this is negligible in this day an age, I respect and want to preserve the opportunity to squeeze the living shit out of a machine.
@JonatasAm
@JonatasAm 4 ай бұрын
That's not exactly true. Behind de scenes the compiling could simply do a x-1 on all indexes, and fot everything back to a byte So you write code 1 -> 256, compiler transform in 0 -> 255 on making the C/bin code The whole point I here, is that we human count from 1, not 0. But computers are counting machines, and would do however we say so
@astral6749
@astral6749 4 ай бұрын
@@JonatasAm So more abstractions then?
@JonatasAm
@JonatasAm 4 ай бұрын
Yeah, that's the whole point of high level languages anyway. Make your writing better for humans, arguably write less Some fixed action/subtraction would be nothing on that So abstract away the "subscription" on indexing, just as TJ said Let the "memory pointer offset" to low level (C, rust, etc) if it's really a thing Honestly it has when i was said to to think on index as offset in a python course back in the day, that this really clicked to me to start at 0. But in retrospect, it should not even be necessary "at this scale"
@NilEoe
@NilEoe 4 ай бұрын
I feel rust could do the abstraction, since you’re usually not dealing with raw pointers anyway
@sub-harmonik
@sub-harmonik 4 ай бұрын
tbf lua does all kinds of weird stuff with arrays and that makes it less relevant. Idk about these days but around 5.1 array elements were only contiguous in memory if initialized like {3, 5, 1, 6}. Everything else after initialization was put in a hash map segment.
@driden1987
@driden1987 4 ай бұрын
The big 0-index conspiracy, we’ve all been tricked
@teej_dv
@teej_dv 4 ай бұрын
WAKE UP SHEEPLE
@jamesarthurkimbell
@jamesarthurkimbell 4 ай бұрын
The Zero Index Conspiracy, by John Grisham
@Kane0123
@Kane0123 4 ай бұрын
I went to chat gippity for a John Grisham title and blurb. Title: "Code Zero" Blurb: In "Code Zero," John Grisham delves into the clandestine world of programming, where a massive conspiracy threatens to upend the very foundation of coding itself. When software engineer Alex Parker stumbles upon evidence suggesting a covert operation to enforce zero indexing over the traditional one index, he finds himself thrust into a perilous journey to uncover the truth. As he races against time, dodging ruthless adversaries at every turn, Alex must navigate a web of deceit, betrayal, and high-stakes tech espionage to unravel the conspiracy before it's too late. With the clock ticking and the fate of programming hanging in the balance, "Code Zero" is a heart-pounding thriller that will keep readers on the edge of their seats until the final, shocking revelation.
@eugbondarev-private
@eugbondarev-private 4 ай бұрын
Like if you just wait for Primeagen to react
@Kane0123
@Kane0123 4 ай бұрын
He'll have an L take. Classic Prime L Take
@IDontReadReplies42069
@IDontReadReplies42069 4 ай бұрын
why would i do that, hes like the least knowledgable coding youtuber. LLL is way better
@FGB64
@FGB64 4 ай бұрын
A long, long time ago I worked in Pascal, which made you specify both the lower and upper indexes of an array. e.g. "array[1..100] of integer" specified a 100-element array with indexes ranging from 1 to 100. This seemed great at first. You could use whatever convention worked best for the problem at hand. In practice this only worked in simple cases. As soon as you started doing any arithmetic with array indexes you would wind up having to add "- 1" to a lot of expressions (and a lot of comments stating that the "- 1" was just there to may the array arithmetic work). Eventually, I just gave up and started using 0-based indexing everywhere just so I wouldn't have to worry about whether or not I needed to adjust the index.
@marcusaureliusf
@marcusaureliusf 4 ай бұрын
Now you need -1 to get the last item. And +1 to print the list to the user.
@VivekYadav-ds8oz
@VivekYadav-ds8oz 4 ай бұрын
Do you have that feeling where you REALLY want to correct someone but you know they're joking, but you NEED to get your points out there but don't wanna be a wet blanket? That's me rn.
@sodiboo
@sodiboo 4 ай бұрын
One argument for 0 that isn't just because of underlying pointer arithmetic is multidimensional arrays. If laid out row after row, then the flattened index is just `row * cols + col`, as opposed to `(row - 1) * cols + col` or for multidimensional arrays, generally `(sum for all ranks i of (idx_i - 1) * (product for all ranks j < i of len_j))) + 1)` as opposed to just removing all the off-by-one adjustments. this is because array indices are inherently *offsets* from the start, something which is isn't inherently linked to the underlying representation. When displaying to humans, i agree that starting from 1 is more natural. But that's not a good reason to use it in code, because there are technical reasons to use 0, and they go beyond "oh that's what the underlying operation actually does and it's a few nanoseconds faster this way". Arithmetic on them is easier with 0-based indices, even if slightly less intuitive when performing no arithmetic.
@СергейМакеев-ж2н
@СергейМакеев-ж2н 4 ай бұрын
Look at what the Roc language (currently in pre-alpha) did to avoid those off-by-1 errors: List.range { start: At 2, end: At 5 } # returns [2, 3, 4, 5] List.range { start: After 2, end: Before 5 } # returns [3, 4] List.range { start: At 6, end: Length 4 } # returns [6, 7, 8, 9] Those "At" and "After" are not some magic keywords, they are perfectly ordinary algebraic type instances.
@MarcelRobitaille
@MarcelRobitaille 4 ай бұрын
You make some good points, but there is no argument when you get into modulo arithmetic. I did a project where I had to program a robot to put an item in 1 of 4 containers. Each container could hold 20 items. Container index is i // 20. Slot in container is i % 20 (not (i -1) % 20 + 1). Offset to love the robot from the first slot: i * slot_spacing. This is not just a subscript. It really makes sense to count slots from 0. Iterators don't save you. Not only are they not supported by the robot language, but you need the index to offset the robot from the reference position into the right slot. Also, with 1 index, you either do offset = (i - 1) * slot_spacing or your reference position is not the first (or any valid) slot.
@mateusantonio9303
@mateusantonio9303 4 ай бұрын
Convinced me with Brazil mention
@anderdrache8504
@anderdrache8504 4 ай бұрын
0-based indexing is less of an abstraction and is therefore simpler for the hardware and the compiler and is also easier for indexing multi-dimensional arrays and easier to bounds-check (i < len instead of i > 0 && i
@fl4shi238
@fl4shi238 4 ай бұрын
In mathematics subscript zero is almost always the base case. 1-based is not as intuitive as people think: Calendar years are ordinal numbers, but people still think that year is the first year of the decade if year MOD 10 == 0...
@jsonkody
@jsonkody 4 ай бұрын
measuring things like distance, time etc is not indexing in my head - indexing is pointing at element in array. When you need to measure you need to start from zero. I think people confuse those two different things together as one thing
@skaruts
@skaruts 4 ай бұрын
Yes, all counts actually start at 0, but most people don't realize they just ignore the 0 because in many cases it's simply not useful. However, don't conflate counting with indexing. They're not the same thing. Everybody does that, but then that's the only reason why this debate isn't a dead horse with everyone realizing 1-indexing only causes problems and doesn't work with indexing math.
@ZackaryReaves
@ZackaryReaves 4 ай бұрын
In matrix notation, indexing starts at one. Checkmate atheists.
@umka7536
@umka7536 4 ай бұрын
I agree. Even with Python range(10) pisses me off because I intuitively expect 1,2,3,4..,10. But it gives me 0,1,2,3...,9. And it works this way and I know that it works this way, but it always makes my brain stutter a bit.
@skaruts
@skaruts 4 ай бұрын
You expect that because no one ever told you that indexing and counting are different things and you shouldn't get them mixed up. And this video is only hammering on that crucial mistake. Python works the way it should, because indexing math only works properly with 0-indexing. index = x+y*width //
@timwmillard
@timwmillard 4 ай бұрын
Love this TJ, I agree. 0 subscription for low level like in C, matches pointer arithmetic. 1 indexing for high level like Lua, is more natural. All languages don’t need to be the same. That’s the point of having different languages.
@diadetediotedio6918
@diadetediotedio6918 4 ай бұрын
The problem is the friction of having this is some languages and not in others. Just like if Ptua (a new hypothetical language) come and say "now we use negative indexing, it is just weird.
@timwmillard
@timwmillard 4 ай бұрын
@@diadetediotedio6918 languages are different and that’s a good thing. I like that you to need to change your thinking when switching languages.
@skaruts
@skaruts 4 ай бұрын
*_"1 indexing for high level like Lua, is more natural. "_* No it isn't. You'll still have to do indexing math, and it only works with 0-indexing. index = x+y*width //
@LTibor2012
@LTibor2012 4 ай бұрын
Fortran, Lua, Julia, Matlab, Gnu Octave. All make1 based indexing and it is good so.
@skaruts
@skaruts 4 ай бұрын
Except it's not good. It comes with lots of problems. And you can't compare Lua and Julia with Matlab. I don't think you ever do any indexing math in Matlab, even. If you did, you'd have problems, because it just doesn't work properly with 1-indexing. That's one of the problems of 1-indexing.
@mohaamer6866
@mohaamer6866 4 ай бұрын
My man, you just convinced me in the 0th argument 😂😂
@user-tc2ky6fg2o
@user-tc2ky6fg2o 4 ай бұрын
My index finger sits on the second place (but it is negotionable, could be context-dependent).
@Raspredval1337
@Raspredval1337 4 ай бұрын
exactly!
@Strawberry_Htet
@Strawberry_Htet 4 ай бұрын
He took RTFM(C - specs) seriously.😂❤
@PopescuAlexandruCristian
@PopescuAlexandruCristian 4 ай бұрын
for an array that is kept contiguous in memory and logically has N dimensions with a known strides, and you need to index it, in our world is just : multidimensional_array[column + row * row_stride] In 1 based indexing schemas you have to write a little story. Also historical reasons are non-reasons, we don't use Newton's laws to get to the moon we use Einstein's special relativity. People in the distant past and toddlers are on average dumb than a programmer should be.
@ErrorNaN_1
@ErrorNaN_1 4 ай бұрын
Even in plain english, there is a discontinuity between zero and one indexed counting. How many *years* old is a child during his/her first year of life? Or on a timer, what is the value in the "seconds" place during the first second? Or in 24-hour time, what is the the value of the "hours" place during the first hour? But this logic breaks down once you begin talking about whole items as opposed to partial items. Ultimately, my opinion has changed slightly. Bravo
@drdca8263
@drdca8263 4 ай бұрын
Funny enough, I think in China the age of a newborn is(or, traditionally was?) described as being “1”? Many people have made the mistake of starting from 1 where they should start at 0.
@teej_dv
@teej_dv 4 ай бұрын
Haha nice! The main goal was for people to just give Lua a chance instead of hating just because different array index 😂
@alfred5454
@alfred5454 4 ай бұрын
​@@teej_dvLua is so cool. You can choose to do either 0 or 1 index based arrays.
@kenneth_romero
@kenneth_romero 4 ай бұрын
dude pulled out specification like lawyers pull out law codes. goddamn.
@ojoaoprocopio
@ojoaoprocopio 4 ай бұрын
how do you make terminal slides?
@siaal5001
@siaal5001 4 ай бұрын
I do take issue with the very last point, if you're using a foreach loop anyway, you wouldn't do it on a range, you'd just do it on the array, so it doesn't matter. If you're using a for loop, then 1 based indexing means you have to use i
@andrewmerrin
@andrewmerrin 4 ай бұрын
In the uk the ground floor of a building is zero and the next one up is the 1st and so on.
@M_1024
@M_1024 3 ай бұрын
Hours start at 0. We have 0:00, not 1:01. We can interpret 0:30 as "since midnight, 0 hours and 30 minutes have passed", but what would 1:31 mean? But centries start at 1. And it only brings problems. Year 00XX is ceuntry 1, year 01XX is ceuntry 2 and year 20XX is ceuntry 21. Years also start at 1, and it also brings problems. Year 1 b.c. is year 0, year 2 b.c. is year -1, year 3 b.c. is year -2. Starting at zero also makes it easy to do unit conversion. 1 inch = 2.54 cm therefore 10 inches = 25.4 cm. This only works because 0 inches = 0 cm. But degrees start at -273 C or -459 F. And this again brings problems: if 0 C = 32 F then multiplying both sides by 2 gives 0 C = 64 F. Binary numbers in computers also start at 0. uint8 covers integers 0-255. So if we started indexing arrays from 1, we would get a smaller range 1-255 and 0 would be wasted.
@jackdeespadas
@jackdeespadas 4 ай бұрын
Everybody gangsta until TJ brings out the specification...
@grihabor
@grihabor 4 ай бұрын
At the end of the day what matters is consistency. If most languages use 0-indexing, then you are used to 0-indexing, and switching back and forth will likely cause you to make mistakes.
@tanmaybora359
@tanmaybora359 4 ай бұрын
Exactly
@nullpointer1755
@nullpointer1755 4 ай бұрын
Skill issue. it is really not that difficult.
@SlothWindGod
@SlothWindGod 4 ай бұрын
​@@nullpointer1755 You are the kind of person to release code with memory vulnerabilities into production
@NilEoe
@NilEoe 4 ай бұрын
I’m so fed up with people saying skill issue every second. The issue is not “offsetting is too hard”, it’s just acknowledging it’s an issue
@petaro
@petaro 4 ай бұрын
What presentation software are you using for the slides?
@binary_gaming113
@binary_gaming113 4 ай бұрын
It's the python tool "lookatme"
@ProgrammingLovers
@ProgrammingLovers 4 ай бұрын
The Ring programming language uses 1-based indexing (Because it's a language designed for applications development and high-level stuff) but since we provide extensions to libraries written in C/C++ this requires extra effort at the extension level development to provide the same functions/classes/methods that take 1-based indexing in mind. Also, some developers asked about 0-based indexing and that they want it, so we provided (ZeroLib).
@Kane0123
@Kane0123 4 ай бұрын
Zero means none. Aka Null. Solved.
@benitoe.4878
@benitoe.4878 4 ай бұрын
However Null means Zero in German. 😅
@insentia8424
@insentia8424 3 ай бұрын
When I attended university in Germany, in physics experimentation we were told to start counting with 0 whenever we started the timer for a measurement and counted elapsed cycles. It's easier to get into rhythm that way, because you start counting when you start the experiment, but don't need to adjust the number by 1 afterwards in your calculations. Whenever you need to first get to the starting point, starting counting from 0 makes sense (so you don't need to subtract 1 at the end). Whenever you already are at the starting point, you start counting from 1. So, boldly claiming that nobody ever starts counting from 0 outside of programming is plainly wrong.
@PetriJohanLast
@PetriJohanLast 4 ай бұрын
Screw 0-based and 1-based indexing. I'm interested in learning more about this very-based indexing alluded to at the end.
@skaruts
@skaruts 4 ай бұрын
Sorry, but I'll have to say this is all very incorrect. You're making two very crucial mistakes (among others): 1- you're conflating indexing and counting. They're not the same thing at all. 2- you're forgetting that indexing math exists, and it only works with 0-indexing An array of 10 elements will still have a *count* of 10 elements (10 loop iterations) regardless of whether it's indexed from 0 or 1. Indexing and counting are very different things. The _"first element"_ is a count, not an index. _"Index 0"_ points at the first element. The only thing you got right was the statement _"that's what "indexes" do: they point at things"._ But then you failed to realize that "counts" don't point at things, and are not the same thing as "indices" (the plural is "indices", btw, not "indexes"). This nullifies many of the arguments you made based on this premise. And then the indexing math (converting an index to X and Y coordinates and vice-versa) is what you use to access an element in a 1D array that represents for example a 2D grid of tiles in an RPG, or a 3D chunk of voxels, or a 2D text buffer in a terminal, etc, etc. For example: index = x+y*width //
@zxuiji
@zxuiji 4 ай бұрын
We do though? Any time you say "initial" position you're in fact referring to index 0, when you say N across you're referring to index 0 + N. We absolutely use those terms in addition to 1st, 2nd etc at work, in life, everywhere. In other words "first" is to "initial" as lua's "[1]" is to C's "[0]"
@BryanBrookesSmith
@BryanBrookesSmith 3 ай бұрын
In the UK we label floors of buildings starting with G for ground, and 1 is the next one up etc effectively counting from 0, so it's always felt a little more intuitive to me to start from 0.
@kirillfedtsov
@kirillfedtsov 4 ай бұрын
You can tell that TJ is obviously teaching his kids Python right now
@Erros
@Erros 4 ай бұрын
write the row major order and column major order algorithms for 1 dimensional arrays so you treat the array as if it was a higher order with 1 as the starting index, I would have been in your camp until I came across those algorithms. (even if this video is probably engagement bait)
@Jarikraider
@Jarikraider 4 ай бұрын
I remember thinking the same thing when I was initially learning how to program and in theory I agree. However, with how pointer arithmetic is performed, and it is performed regardless of your programming language, you simply get a 1:1 equivalent by using 0 as the starting index. Using 1 as the starting index would, I assume, require the compiler to subtract 1 from pointer indexing which is a performance hit and is a disconnect from what you write to what you get. I also think the attempt to blur indexing, subscript, and an indicator is dishonest. Any language that utilizes an array implementation, regardless of you calling it a list or a vector, still does pointer arithmetic under the hood. TL;DR, 1 based indexing is more human readable but 0 based indexing is more accurate to what the computer will execute and more performant.
@NickMak-m2c
@NickMak-m2c 4 ай бұрын
Well if you really want to follow that to fullest extent that you could just write in machine code, the rule of thumb is not what's more honest to what the computer's doing, it's what's more honest to what the human's doing. I don't think converting 1 to 0*n really matters since you're converting anyway, it's not like that actual zero in the IDE is going directly into memory anyway.
@valley-artifact
@valley-artifact Ай бұрын
that's not true, in higher level languages like python and javascript arrays don't represent contiguos regions of memory, the low-level things they're implemented on top of aren't c-style arrays they're linked-lists and hash-maps, that's what TJ is talking about.
@user-cx5jj2yv3p
@user-cx5jj2yv3p 4 ай бұрын
Came from PrimeTime's video, absolute legendary walkoff! Subscribing now
@fistoflegend7846
@fistoflegend7846 4 ай бұрын
I spend most my time in C/C++, but I do use python pretty regularly as well. Honestly, when you ask what number to index the third item in a python list, I do instinctively think '2' because I spend so much more time in C/C++ than in any other language. My brain would melt if I had to remember that python is different (although, I do understand what you're saying when you say that ARE different). Also, I sometimes index a python list using a boolean converted to an int, which is pretty handy, although many people would say it's more obvious code if you simply use a branch.
@Om4r37
@Om4r37 4 ай бұрын
but python uses 0 based indexing?
@fistoflegend7846
@fistoflegend7846 4 ай бұрын
@@Om4r37 I know, I'm saying why I like 0 based indexing better than 1 based.
@samuelswatson
@samuelswatson 4 ай бұрын
I dunno, I write a fair amount of code in both 0-based-indexing and 1-based-indexing languages, and at a certain point the context-switching cost becomes pretty low in my experience. In other words, I don't think your brain would melt. Overall, I agree that counting discrete items starting from 1 makes the most sense, but there are use cases where 0-based indexing is much more ergonomic (for example, around operations that involve the modulo operator).
@amj864
@amj864 4 ай бұрын
Those who complain about 1-indexing are telling the world they just have skill issues
@dziuaftermidnight
@dziuaftermidnight 4 ай бұрын
1-based indexing breaks apart on so many places that you don't even think about until you start using it.
@rodionbykov
@rodionbykov 3 ай бұрын
It's first child who is born and first dollar what is earned. We don't count from 0 in real world, neither should we in high-level language. Describing real world with high-level language means we are not bound by inner mechanics of the compiler. Also, most of types now are linked lists or other recursive type, not array as C understands it. One of the languages using 1-based indexes is Adobe Coldfusion, and searching for element in arraylist or for entry in a string is easy - function either returns 1-based index of the element, or returns 0 if nothing is found. In turn, 0 can be interpreted as 'false'. Checking if something exists in array becomes easy. Thank you for video!
@MarcelRobitaille
@MarcelRobitaille 4 ай бұрын
As a compromise let's use 0.5
@teej_dv
@teej_dv 4 ай бұрын
Your terms are acceptable
@jesse9999999
@jesse9999999 4 ай бұрын
I really feel for your wife if you can pull up receipts with the c docs in such an insane way here. Arguments must be a nightmare. Especially the 0th argument.
@onee
@onee 3 ай бұрын
Thank you! 0-based indexing never made sense to me from a language perspective. Even if a clock shows 0:00 (that's 12 AM for you Americans), people say 12 o'clock or midnight. What even makes less sense to me is range in Python. Why the hell would you want to loop from 0 to 9, but write 0 to 10? Which human says, count from 1 till 10, but don't count 10. That doesn't make sense at all. It is only programmed like that, because once upon a time Edsger Dijkstra thought that it was beautiful. That makes zero sense.
@ZisulinMorbrot
@ZisulinMorbrot 4 ай бұрын
What's the font used in the presentation? I need that in my life.
@Becoming-Human
@Becoming-Human 4 ай бұрын
In my opinion, one of the strongest arguments for 1 based indexing, at least in Python, would be the following logic, "In Python, if negative indexing starts at -1, then why _wouldn't_ positive indexing start at 1?"
@Ursacke
@Ursacke 4 ай бұрын
TL:DR rebuttal: 0 - - - 1 - - - 2 -| - 1 - | - 2 - |- Intervals vs. indexes. The first interval is between the zeroth and first index. TL: Anecdote in support of 0-based: When I learned to drive I was given a safety tip: Keep 2 seconds between yourself and the car in front. You pick a stationary landmark, wait for the car in front to reach it, then start counting. It's a reasonable safety tip regardless of speed, and it's easy to remember. But when I spoke to different people I got different numbers. Some said 3 seconds would keep you safe, and some said 2 seconds was enough. Well, if you go by your stopwatch you need to count 3 times in order to cover an interval of 2 seconds. So what were all these "2 seconds" people telling me? Well, they just plain got the advice wrong. Who knows how many tailgaters are on the roads these days because they were told to count to 2 instead of 3? Still, I'm not convinced intuition is actually an issue so long as you have it demonstrated to you. Nobody is confused by the 00:00.00 on a digital clock showing midnight, or on a stopwatch, and if we used them in driving schools we might have safer roads. So basically I'm saying you're killing people with this 1-based lizard brain business.
@teej_dv
@teej_dv 4 ай бұрын
i would still say "continuous time is not the same as a discontinuous index" :)
@boriscat1999
@boriscat1999 3 ай бұрын
The last character in a C [null-terminated] string is always 0. Now you will never make an off-by-one mistake with C strings again.
@AtomToast
@AtomToast 4 ай бұрын
I think there is value to just keeping things consistent in how they are done. Even if 0 based indexing isn't perfect, it just being the standard everywhere makes interop a lot easier. Otherwise we'll end up with some big/little endian mess
@tanmaybora359
@tanmaybora359 4 ай бұрын
Exactly
@guccislinger
@guccislinger 4 ай бұрын
2 reasons why it shouldn't be 1: 1. To put 256th element into array you'll need to use 2^16 bit as an array pointer because it won't fit into 2^8. 2. To calculate a pointer offset you need to do additional computation for every array access by index.
@hamzarashid7579
@hamzarashid7579 4 ай бұрын
My life has been a lie
@ollllj
@ollllj 3 ай бұрын
no youtube, just stop suggesting me such obvious nonsense crap. 1-based indexing does more harm than flat earthers.
@anomaliespatiale6023
@anomaliespatiale6023 4 ай бұрын
Thank you for making this video! Julia and R data analyst here. 0-based indexing makes NO sense in data science. Even though I've been trained on 0-based indexing in languages such as C and Java, I'm not missing it in the least. In my day job, I count *things*, and don't refer to memory offsets. I give pretty much the same arguments that you do to naysayers, especially about the subscripts (I've been calling them offsets). It baffles the mind that a dynamic high level language like Python is 0-based. In fact, I think I use alternatives in big part because of this, even though Python is such a huge language in the data science world. Programmers who use Pandas just suffer so much. Since people are most likely to be convinced by you than me, I can just send them over to this video... So thanks!
@roberttuck4768
@roberttuck4768 4 ай бұрын
0-based index means any time you need to use a null-terminated string, the length of your string is the length of your string
@thistent
@thistent 4 ай бұрын
How many steps do you need to take to get to your initial position, TJ? How many steps do you have to take to get to your third position?
@Bilessati
@Bilessati 4 ай бұрын
The fact that a whole population of ostensibly smart people have been convinced to mentally remap the symbols for the integers to disagree by one with the set of symbols they'll teach their children is frankly frightening.
@rysw19
@rysw19 4 ай бұрын
“Subscripting” refers to mathematical indexing of a sequence of some kind, which are written as subscripts. These begin by convention with either 0 or 1. Therefore both are correct. QED.
@igor710
@igor710 4 ай бұрын
Love the Brazil mentioned thing
@andrewlewis7610
@andrewlewis7610 3 ай бұрын
TJs contact book stores all the addresses of his friends and family’s neighbors. He drives to the neighbors address then walks 1 house next door. You tell TJ that you start on the first letter of the alphabet and ask him what’s one letter away. He says “A”. You ask TJ how deep your 5ft deep pool is and he says 6ft because he always starts counting at 1 then adds 5 more feet more.
@pedrinhodam
@pedrinhodam 4 ай бұрын
the amount of times i have to subtract one from the length of the array annoys me so much
@grexe
@grexe 4 ай бұрын
Good points and basically agree, but the example with the index finger depends on the culture - in Germany and Austria, we indicate "1" with our thumb 😉
4 ай бұрын
"Not for children" -- so is whiskey, but I'm still enjoying it. "Not intuitive" -- dude, by your own reasoning we've been using it since 300 BC. That is ENOUGH time to get used to the idea of zero being a thing.
@nekoill
@nekoill 4 ай бұрын
Didn't even watch a minute, and I already agree. Indeed, never in my life have I started counting from zero, ever. Makes perfect sense.
@darylbarnes9413
@darylbarnes9413 4 ай бұрын
Kids are actually taught to count from zero. You just don't say zero out loud but you certainly raise your balled fist "indicating zero" then you extend your index finger and say "one". So it's actually two steps to get to "1" not one. You start at zero. This is how the universe works.
@REOsama
@REOsama 4 ай бұрын
As someone who designs processors and memory, everything in my world is zero based, that's just how computers work, and it was people from the very low level world that wrote C, then people from the C world wrote the other languages, so that mindset just carried on
@gboncoffee
@gboncoffee 4 ай бұрын
BRAZIL MENTIONED!!!!!!
@teej_dv
@teej_dv 4 ай бұрын
BRAZILLLLLLL 🇧🇷
@jonathanfeinberg7304
@jonathanfeinberg7304 4 ай бұрын
We count human life 0-indexed. We have 0 year old humans.
@JonatasAm
@JonatasAm 4 ай бұрын
I've always thought like that since beginning. "The frickin computer should do the x-1 for me back there, damn it". Had to teach some teenagers once, and starting from 0 was definitely the hardest part for them.
@skaruts
@skaruts 4 ай бұрын
If starting from 0 is hard for you then you're probably in the wrong classroom. Indexing math only works with 0-indexing, and modulus math is much simpler with 0-indexing, etc. That's the hard reality. 1-indexing doesn't make anything easier. Not mentioning other problems it brings.
@krasilnikovdo
@krasilnikovdo 4 ай бұрын
I had very similar thoughts in the university before succumbing to the "C industrial complex". It'd be nice to have more 1 based indexing in future languages.
@mikeg9b
@mikeg9b 4 ай бұрын
Julia has 1-based indexing and I prefer it. On the other hand, it uses * (not +) to concatenate strings. The reason they give requires a PhD in math or CS to understand it: "More precisely, the set of all finite-length strings S together with the string concatenation operator * forms a free monoid (S, *). The identity element of this set is the empty string, "". Whenever a free monoid is not commutative, the operation is typically represented as \cdot, *, or a similar symbol, rather than +, which as stated usually implies commutativity."
@teej_dv
@teej_dv 4 ай бұрын
sounds like julia hahaha
@peppybocan
@peppybocan 4 ай бұрын
I am fairly sure as most of the time, you are just click/rage baiting here. But I am going to put my two cents as if it was a real and serious video. 1:06 TJ starts actually belittling everybody because children books start with 1, so must you. #skillIssue ? It "feels different". I am sorry, but facts don't care about your feelings, TJ. 3:36 - again, a little bit of technicality/lingo issue. "Subscripting" is indexing. No matter how you slice and dice it. The act of adding a subscript to a sequence, is indexing. But let's not go into the well-ordered and partially-ordered sets. The fact that the C spec says that counting starts from zero means literally that the indexing/sequence starts at zero. Look, people who wrote these books are/were mathematicians, they use subscripts all day every day. In computer typesetting lingo there is not really a subscript in our code. Our editors are row and column oriented, and you should know that. Our code does not read like a LaTeX typesetted PDF file. I also don't think the offset is the same as the index. Index = 1,2,3... offset would be a memory location (byte/word) aligned, for that you are missing a sizeof the element to do the jump. Doing (address + 1, will yield a segfault on architectures that don't handle misaligned memory). But I think this is a matter of clearing up the definition of an offset (index). your Python dictionary example is also wrong. We are reusing (overloading) the same syntax for "indices" as the keys for the hash function. Maps (or sets) mathematically speaking have no first value, unless you impose ordering restriction on the set. Appeal to intuition. Amazing logical argument. Refutal to your string example. How would you get an address of an array in a C-like language which is also 1-based? Would you say &array is 0x0080 and &array[1] is also 0x0080 ? How would you translate that to pointer arithmetic? &(array + 1 - 1) ? I could see that working if and only if arrays are no longer a simple list of items, and array becomes a proper container/data structure. But until that point I don't see the way this working. Conclusion is also wrong. Sometimes you gotta do what you gotta do. Iterators ain't the hail Mary. I don't like appeals to traditions, but your arguments are on the same level.
@jimreem1693
@jimreem1693 4 ай бұрын
Regarding intuition, mathematics, etc, this reminds me of having my mind “expanded” the first time I encountered infinite sums indexed from -∞ to +∞. Not that that applies directly except perhaps with infinite-sized arrays 😂 but it definitely reminds one that there are more ways to count in heaven and earth, Horatio, than are dreamt of in your programming language. (hamlet.js, 1.5.165-66)
@nitsanbh
@nitsanbh 4 ай бұрын
Not everything starts at 0. When you measure distances you start at 0. Where you stand is “0 distance”, and an object can be “2 meters away”. Array index is distance from where the array starts
@gokukakarot6323
@gokukakarot6323 4 ай бұрын
True, they called Zeroth law of thermodynamics, because the first 3 was already existed. Although for Indians, 0 is of cultural significance
@iozoz585
@iozoz585 4 ай бұрын
what did you use for make the presentation?
@binary_gaming113
@binary_gaming113 4 ай бұрын
The python tool "lookatme"
@jhonatanjacinto
@jhonatanjacinto 4 ай бұрын
I think everyone who starts learning programming in a zero-based index language, asks this question at least once: "Why ZERO is the first item?" And after getting used to it, stops bothering. But TJ is right! Zero makes no sense, we're just used to it while learning programming.
@drdca8263
@drdca8263 4 ай бұрын
To the contrary, 1 makes little sense.
@Novacification
@Novacification 4 ай бұрын
Damn! You had a nice little channel here. I guess all good things must come to an end :)
@phist1229
@phist1229 3 ай бұрын
Let me introduce you to an alternate universe where counting from 1 is the norm: music theory, which for some time has annoyed me directly as a consequence of counting from 1 where it probably would be better to have counted from 0. For example, when you have a song in 4/4 (meaning there are 4 beats in a bar) and you want to start in the middle of a bar, which beat do you start on? The answer is... 3, as beats begin from 1. If beats were counted from 0, the answer would be 2, which is to me the more intuitive one (or maybe its just my industrial C complex). Similarly for pitch intervals, a ""zero""-length interval from C to C is a 1 or a "1st", and going an octave above gives you an 8. So are there 8 notes in an octave? Well, no, there are really 7, and the 8th one is the first note in the *next* octave. If you want to go an octave and a fourth, the resulting interval is 11 = (8-1)+(4-1)+1. If you instead counted from 0, you would just add 7+3=10 and be done. Also, I know this video isn't too serious, and for the record, I probably wouldn't even care that much if some containers counted indices from 1, but I had to get this out of my system, because you people don't know how good you really have it.
@ssshenkie
@ssshenkie 4 ай бұрын
1-based indexing would be nice so you can just use the length of the array to point to the last item, rather than doing an additional -1
@Coffe789
@Coffe789 4 ай бұрын
and now when you want a random index you go (randi() % len(arr)) + 1. Or if you want to wrap the index as it increments you go idx = ((idx + 1 - 1) % len(arr)) + 1, which is not particularly readable even when you simplify it imo.
@josephangelodelosreyes4206
@josephangelodelosreyes4206 4 ай бұрын
3:49 I freakin knew he's gonna pull this up
@teej_dv
@teej_dv 4 ай бұрын
called it
@dedebenui
@dedebenui 4 ай бұрын
I'm glad that unlike 0-based indexing, iterators are widely used in everyday life and taught to children at a young age (everybody does it!), they're way more intuitive :)
@zaneearldufour
@zaneearldufour 4 ай бұрын
0-based indexing is honestly the worst part about working with numpy, b.c. it requires a stupid translation from any math formula
@Onyx-it8gk
@Onyx-it8gk 4 ай бұрын
The four software freedoms: The freedom to run the program as you wish, for any purpose (freedom 1). The freedom to study how the program works and change it so it does your computing as you wish (freedom 2). The freedom to redistribute copies so you can help others (freedom 3). The freedom to distribute copies of your modified versions to others (freedom 4).
@ukyoize
@ukyoize 4 ай бұрын
Don't those actually start from 0?
@Onyx-it8gk
@Onyx-it8gk 4 ай бұрын
@@ukyoize Yes. I guess you didn't get it.
@lhpl
@lhpl 4 ай бұрын
Freedom -1 - the freedom to index your arrays as you damn well please! Array[lwb .. upb] of T !
@ramchandramodak638
@ramchandramodak638 4 ай бұрын
Dude what tool are you using for the slideshows?
@ceigey-au
@ceigey-au 4 ай бұрын
I feel like 50% of introducing someone to coding is just getting people past all the arbitrary decisions made with collection types in general (along with other language features and idioms that don't correlate to plain English but instead are based on some in-group traditions that're only 70 years old and were already tenuous until like 40 years ago).
@harryporter3116
@harryporter3116 4 ай бұрын
Big agree, pretty much every other argument is honestly just an appeal to tradition.
@Kane0123
@Kane0123 4 ай бұрын
It’s the way we’ve always done it. Turns out programmers are just as bad as business users ;)
@diadetediotedio6918
@diadetediotedio6918 4 ай бұрын
And tradition is good, which is not a bad thing to appeal to it sometimes. Curiously the same applies to backwards compatibility, it is about "tradition" in some senses but it is OBVIOUSLY important. Also, it makes sense in the context of computer programming and arrays.
@harryporter3116
@harryporter3116 4 ай бұрын
​@@diadetediotedio6918 Sure, there's nothing wrong with this tradition per sé, but any argument that zeroed indices ought to be used because that's the precedent (outside the memory offset context, where they make sense) just reads fallacious for me. I don't really have a problem with either in practice, but I do think that one-based indices (again, outside the array subscripting context as per the video) are semantically correct. I'm not sure if I'd equate zeroed indices-which are strictly a semantic design decision-with backwards compatibility, either, because the latter isn't maintained out of intertia or tradition, but out of obvious necessity. It's completely unnecessary to continue using zeroed indices in new languages, for example. It's a trivial difference either way, of course 🙃
@XenoCrimson-uv8uz
@XenoCrimson-uv8uz 4 ай бұрын
Tradition is good but what wrong with making it simpler?
@chethelesser
@chethelesser 4 ай бұрын
I haven't realised that people actually advocated for 0-based instead of admitting it's an unfortunate tradition and a force of habit
@thomassynths
@thomassynths 4 ай бұрын
So TJ's argument basically boils down to - I still think about math as an adult the same way I understood mathematics as a toddler: finger counting. - It took humanity a while to realize zero is a thing. Gosh golly it must still be a hard concept for modern society to grasp. Bro, if you have a hard time with the notion of zero, you are going to have harder issues with programming than subscripts.
tree-sitter explained
15:00
TJ DeVries
Рет қаралды 85 М.
You Are WRONG About 0 Based Indexing
25:02
ThePrimeTime
Рет қаралды 290 М.
哈莉奎因怎么变骷髅了#小丑 #shorts
00:19
好人小丑
Рет қаралды 52 МЛН
escape in roblox in real life
00:13
Kan Andrey
Рет қаралды 85 МЛН
БЕЛКА СЬЕЛА КОТЕНКА?#cat
00:13
Лайки Like
Рет қаралды 1,8 МЛН
🍉😋 #shorts
00:24
Денис Кукояка
Рет қаралды 3,3 МЛН
Writing an interpreter... in OCaml?!?
17:10
TJ DeVries
Рет қаралды 38 М.
I Rewrote My Entire Neovim Config...
11:09
TJ DeVries
Рет қаралды 44 М.
Is it still worth it to learn to code? (I think so)
14:59
TJ DeVries
Рет қаралды 27 М.
The Only Video You Need to Get Started with Neovim
34:00
TJ DeVries
Рет қаралды 375 М.
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 78 М.
1000 Players - One Game of Doom
15:42
ThePrimeagen
Рет қаралды 232 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
why do void* pointers even exist?
8:17
Low Level
Рет қаралды 365 М.
Beginners Should Think Differently When Writing Golang
11:35
Anthony GG
Рет қаралды 110 М.
Vim Tips I Wish I Knew Earlier
23:00
Sebastian Daschner
Рет қаралды 67 М.
哈莉奎因怎么变骷髅了#小丑 #shorts
00:19
好人小丑
Рет қаралды 52 МЛН