From where should the index start?
-
In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?
I've designed a script programming language which was intended to be used by entry-level programmers. With that in mind, I've adopted 1-based indexing (both for strings and arrays) in the hope of making it simpler to understand. My reasoning was that it is easier to remember that 1st element is [1] and not [0] for unskilled coders. Now that a lot of code has been written in that language, I really repent of my choice. On average I see that code is slightly longer than it would be if it was 0-based, and often wrong access to [0] element causes problems. At a certain point I decided to revert to 0-based indexing but it was too late because there was too much code to fix. So my advice is, stick to 0 indexing, especially if your audience has background in c/c++/java/c#.
-
I've designed a script programming language which was intended to be used by entry-level programmers. With that in mind, I've adopted 1-based indexing (both for strings and arrays) in the hope of making it simpler to understand. My reasoning was that it is easier to remember that 1st element is [1] and not [0] for unskilled coders. Now that a lot of code has been written in that language, I really repent of my choice. On average I see that code is slightly longer than it would be if it was 0-based, and often wrong access to [0] element causes problems. At a certain point I decided to revert to 0-based indexing but it was too late because there was too much code to fix. So my advice is, stick to 0 indexing, especially if your audience has background in c/c++/java/c#.
Seeing all the replies, I think you have provided a very strong PRACTICAL answer. All other are just saying on their own thoughts and not going deep to the actual problem. However, I still would like to use 1 based index. Presently I could be wrong. I would & should first check some of my programs using 1 based index instead of 0 based index. I don't think using 1 based index would create any problem. You may be facing the problem because the entry-level programmers will move on to other higher generation languages in future and thus, from starting, they would think of using the same methods as provided in the higher generation languages. And I don't think using a 1 based index would create any noticeable time-delay because 1 based index would be only for programmer but in the machine language, it can use 0 based index and this conversion can be applied at compile time and the execution speed will not be affected. Programmers may not need to became comfortable to computer language/computer, but the languages can be designed to become comfortable to programmers. In common sense, if I say someone (as I am a teacher, I often say this to my students) to display value of the 1st element of an array, what he/they would understand? the first element of array which is array[0] OR array[1]? I then have to say display value of the 0th element! Isn't this strange (actually, weird)?
-
In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?
I Was going to say I really don't care, but thwn remembered my stint in Rocky Mountain BASIC (in HP technical computers for engineering and test) where one could use any set of indices you wanted. I ended up settling on 0 for all arrays because it made bound checking a lot easier. Remember the 3-week rule: Agter 3 weeks you'll look at you're code and mutter "Why the hell did I do that and what does it do?". It's a lot easier if there is consistency in your code. After years of C/C++/C# and recent Ruby I have no problem with 0 based indices.
-
In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?
I would favor a 1-based index instead of zero based. "First" is "1st" not "0st". Humans automatically think of first as being #1. No blue ribbon ever had a "0st" on it. Back in the day when BASIC allowed us to choose how to index, I always chose one-based indexing. And, I've always thought it a bit clumsy that a For-Next loop had to stop at Count-1 instead of Count.
XAlan Burkhart
-
I would favor a 1-based index instead of zero based. "First" is "1st" not "0st". Humans automatically think of first as being #1. No blue ribbon ever had a "0st" on it. Back in the day when BASIC allowed us to choose how to index, I always chose one-based indexing. And, I've always thought it a bit clumsy that a For-Next loop had to stop at Count-1 instead of Count.
XAlan Burkhart
Alan Burkhart wrote:
"First" is "1st" not "0st".
Actually is "0th" :P
Alan Burkhart wrote:
And, I've always thought it a bit clumsy that a For-Next loop had to stop at Count-1
Don't use For Next. (I would go as far as to say don't use a language that supports it;)) Use for loops that have end conditions and your end condition should be Less than the count (not less than equal) Lastly,
Alan Burkhart wrote:
Humans automatically think of first as being #1
Are you saying that Mathematicians are not human?? ;)
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
Simply put, it makes sense to have it start at 0. The array variable will be a pointer to a memory address
p
and each item is an offset from there so, for itemn
where each element is sizes
the memory address isp+n*s
. If you want 1 based arrays then the element would be found atp+(n-1)*s
. Which is easier to compute?
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
Nagy Vilmos wrote:
Simply put, it makes sense to have it start at 0.
Opinion.
Nagy Vilmos wrote:
The array variable will be a pointer to a memory address
p
and each item is an offset from there soWrong. First that point only applies to the first value. Any other value in the array needs to be offset anyways. The advantage is so trivial that it probably isn't possible to measure it even in somthing like C using a byte array (noting that access is to the entire array not just the first byte.) Second the point of a modern programming language is to distance oneself from the actual implementation on the machine.
Nagy Vilmos wrote:
Which is easier to compute?
I write financial systems that communicate with databases and many different service providers. There is no possible way that would ever have any impact on any part of a system like that. And many problem domains are like that. There are some problem domains in which some functionality might actually be impacted by that. In that case then one should use C or even assembler (or mixed mode) to do just that.
-
In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?
I skimmed through this whole thread to see if there was any mention of Dijkstra, but didn't see any so here it is: http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html[^] Personally, I prefer 0-based indexes. It makes code dealing with index comparison less clumsy for me.
-
Alan Burkhart wrote:
"First" is "1st" not "0st".
Actually is "0th" :P
Alan Burkhart wrote:
And, I've always thought it a bit clumsy that a For-Next loop had to stop at Count-1
Don't use For Next. (I would go as far as to say don't use a language that supports it;)) Use for loops that have end conditions and your end condition should be Less than the count (not less than equal) Lastly,
Alan Burkhart wrote:
Humans automatically think of first as being #1
Are you saying that Mathematicians are not human?? ;)
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
Collin Jasnoch wrote:
Actually is "0th" :P
Well, see. I said it wasn't 0st. :)
Collin Jasnoch wrote:
Don't use For Next. (I would go as far as to say don't use a language that supports it;))
I prefer
For Each
. But on occasionFor Next
is appropriate for whatever I'm doing. And I'm one of "those people" who use VB.Net.Collin Jasnoch wrote:
Are you saying that Mathematicians are not human??
I have been advised by counsel that it is in my best interest to invoke the privilege provided in the 5th Amendment of the US Constitution. ;)
XAlan Burkhart
-
Rob Grainger wrote:
We have always begun counting at 1, we do not begin at zero but omit it. That's an incredibly wierd outlook.
Actually it is a scientific way of looking at it.
Rob Grainger wrote:
I don't think we're omitting the zero - it was never there to begin with. Zero was introduced much later as a concept then counting, and is one of the most significant developments in mathematics, but its not involved in counting.
No it was not introduced at the same time as counting. "Hey Ugh. Get me 3 sticks for fire".... "1.. 2... 3" Counting has been around for a while, but math proofs and formulas which require enumeration did require it and still do (ohhh wait... Is that one computers are doing??? Math formulas? Hmmm I thought it was just for perrty pictures of Salma Hayek) If you are familiar with math proofs you should know the most common value that you are converging from or to is 0. If you are using a 1 base index (or counting and ommitting 0) you will make the algorithms more complicated. Here is another way to show we are indeed omitting in human language. If you write down "10", we know you mean "10". A computer however will use the data before it. If you did nothing to the data it will not be "10" but could be 26310 or 28510 and so on and so on. Now I know what you are thinking. Compiler handles that why can't it do this for us. Because wether you wrote "10" or "0010" has absolutly no effect on the algorithm. You starting your array from 1 does. If you do not grasp that you may need to study mathematics a little more.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
I absolutely disagree, and would need to see good evidence that counting began with a concept of zero. Types of number: Cardinal: counting numbers 1, 2, 3, ... Ordinal: place numbers 1st, 2nd, 3rd, ... Natural: 0, 1, 2, 3, ... Integers: ..., -3, -2, -1, 0, 1, 2, 3, ... Number representation: Addition Systems: Roman Numerals are the best known example: MCXXIX Position Systems: These require a zero: 3501 Our use of the position system derives from the Hindu system, through the Near East (hence Arabic numerals). The introduction of zero as a number occurred around 800AD. Are you seriously proposing people didn't count until then? Sources: VNR Concise Encyclopedia of Mathematics, Kustener and Kastener. Wikipedia.
-
Simply put it should be 0 or it becomes more confusing. Many have pointed out why it is 0 and to not grasp that is then dangerous for you to program. The reasoning is simple. You are working with a system that reconizes 0 implicitly. There is no changing that. The reasoning for this is that 0 does exist. In human languages we tend to use 0 implicitly. This means that unless I say otherwise assume 0. Example, I want to order 5 Chicken strips. The waitor will hopefully not bring me 1 of everything else on the menu. Collections being 0 based indexing are indirectly related to this. Define Chicken stripsBasket (implicityly I have 0 chicken strips but I have my basket) Fill basket with 5 chicken strips Now to access your chicken strips you have a reference to the basket... Not your friends basket... Not the basket on the table next to you. Your basket. For efficiency referencing the basket gets you a chicken strip, unless you wanted to actually eat the basket. By requiring 1 based indexing you are adding a step to the chicken strip access which when compounded with the complexity of systems we create today would have horrible performance (and for no reason). If one does not grasp why computer languages use 0 based indexing (more importantly that they do), one should not be a programmer IMHO.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
I beg to differ. When I order 5 Chicken Strips (unlikely, as I'm veggie, but I digress), I am not referring to 0 of anything at all. Indeed, this is often explicit when it need to be :- "Can I have a full English? but hold on the beans" for example. Referring to none, or zero of something is as explicit as any other reference, but occurs less frequently. I refer to you my post elsewhere in this discussion on the history of zero. tl;dr; it did'nt exist as a number until the year 800CE.
-
I absolutely disagree, and would need to see good evidence that counting began with a concept of zero. Types of number: Cardinal: counting numbers 1, 2, 3, ... Ordinal: place numbers 1st, 2nd, 3rd, ... Natural: 0, 1, 2, 3, ... Integers: ..., -3, -2, -1, 0, 1, 2, 3, ... Number representation: Addition Systems: Roman Numerals are the best known example: MCXXIX Position Systems: These require a zero: 3501 Our use of the position system derives from the Hindu system, through the Near East (hence Arabic numerals). The introduction of zero as a number occurred around 800AD. Are you seriously proposing people didn't count until then? Sources: VNR Concise Encyclopedia of Mathematics, Kustener and Kastener. Wikipedia.
I think you misread my post. I said 0 did NOT come at the beginnings of counting. It came with mathematics requiring it. Also I pointed out that mathematics are the basis of computational theorems and this is why we have 0 based indexing.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
I beg to differ. When I order 5 Chicken Strips (unlikely, as I'm veggie, but I digress), I am not referring to 0 of anything at all. Indeed, this is often explicit when it need to be :- "Can I have a full English? but hold on the beans" for example. Referring to none, or zero of something is as explicit as any other reference, but occurs less frequently. I refer to you my post elsewhere in this discussion on the history of zero. tl;dr; it did'nt exist as a number until the year 800CE.
I think you missed the point. If you were to write an application for ordering and you define chicken strips collection what do you have? My point is that computer language and human languages are different. In human language we can imply numerous things and this omit details. This is not true of programming languages nor should it be. As I have tried to point out computers require explicit communication since mathematics requires it. Using a 0 base index is in most cases convenient. If it is not the case for your programming I would bet most of your programming deals with UI arrays. These are a small subset of the arrays that exist inside the entire system.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
I skimmed through this whole thread to see if there was any mention of Dijkstra, but didn't see any so here it is: http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html[^] Personally, I prefer 0-based indexes. It makes code dealing with index comparison less clumsy for me.
You have provided a very logical explanation. But this doesn't need to be applied in programming when a programmer is creating code. Zero based subscript could be handled by compiler or by the run-time environment, programmer should be able to use anything (but a standard, could be Zero too) as starting subscript. Programmer should aware that "how the system works internally" but this awareness must not be a must for the programer when he is working in a good programming language. And that's why more and more programming languages are introduced (to ease programming), otherwise Assembly is very good IMHO.
-
In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?
Personally I consider any functions that return -1 (or 0) as an indication of an error but an actual value otherwise as bad design: Indication of an error or a meaningful value shouldn't be represented by one and the same number! Instead, the function result , and the indication of an error should be separated, either by passing an output variable by reference, and using the return value as error indicator only, or by using exceptions. This problem is not at all related to indexing however, just to function design. Independently of that, if I were to design a new language, I would consider who would or should use it. If it is meant mostly as an alternative to existing languages then I might be inclined to stick with the usual semantics of basing everything at 0. OTOH, I find a 1-based index more natural, and therefore it would be more suitable for a language that someone would use to learn programming from scratch.
-
You have provided a very logical explanation. But this doesn't need to be applied in programming when a programmer is creating code. Zero based subscript could be handled by compiler or by the run-time environment, programmer should be able to use anything (but a standard, could be Zero too) as starting subscript. Programmer should aware that "how the system works internally" but this awareness must not be a must for the programer when he is working in a good programming language. And that's why more and more programming languages are introduced (to ease programming), otherwise Assembly is very good IMHO.
I agree. The compiler should - indeed usually must - use a 0-based index. But that shouldn't be a restriction for a high level programming language. For all I care an index range could be any sequence of numbers from a to b, no matter what a and b are - depending on the problem at hand it might even make sense to use negative values! For instance consider an array that stores voltages for a Balance dial on a radio which can only be set to distinctive values: the center value would be 0 (naturally!), values to the left might be -1, -2, -3, etc.., whereas values to the right would be +1, +2, +3,... Now, why would I use indexes from 0 to N (or 1 to N) for the purpose of storing the appropriate voltage levels for each settings? It would be much more natural and easier to grasp if I could just use an Index range of -N to N instead! Consider index values as integral key values that are used to quickly retrieve associated values in a simple map - and what do you get? right, freely definable index ranges. (oh, and please don't nail me if my example of 'voltage levels' being set by a balance dial isn't accurate, I'm a software guy, not an electrician ;) )
-
In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?
Let me reverse your question... Where do you want your index to end? Suppose the character/element you are looking for is the last in the string/list. Should retrieving the value be implemented as: zero based: List[List.Count -1]; One based : List[List.Count];
-
Seeing all the replies, I think you have provided a very strong PRACTICAL answer. All other are just saying on their own thoughts and not going deep to the actual problem. However, I still would like to use 1 based index. Presently I could be wrong. I would & should first check some of my programs using 1 based index instead of 0 based index. I don't think using 1 based index would create any problem. You may be facing the problem because the entry-level programmers will move on to other higher generation languages in future and thus, from starting, they would think of using the same methods as provided in the higher generation languages. And I don't think using a 1 based index would create any noticeable time-delay because 1 based index would be only for programmer but in the machine language, it can use 0 based index and this conversion can be applied at compile time and the execution speed will not be affected. Programmers may not need to became comfortable to computer language/computer, but the languages can be designed to become comfortable to programmers. In common sense, if I say someone (as I am a teacher, I often say this to my students) to display value of the 1st element of an array, what he/they would understand? the first element of array which is array[0] OR array[1]? I then have to say display value of the 0th element! Isn't this strange (actually, weird)?
another 1-based argument is when you tell to display the last element in the array, it's hard to explain to novices it's array[count-1] instead of array[count]. Also consider that sometimes it's the language syntax itself that forces the choice of index numbering. For example in BASIC the FOR...TO...NEXT statement does a "<=" as end condition check, so it's more natural to have 1-based indexing, that is
FOR i=1 TO Count
instead ofFOR i=0 to count-1
. -
In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?
I've thought a bit more about your ideas and suggestions in you original post as well as responses (including my own). I've realized two things: First, as mentioned before, the problem you describe about strings and checking their successful execution is not related to index values at all, or at least shouldn't, and if you consider that a problem and are about to design a new language, it should be easy for you to avoid that trap. For instance you could attach an error flag to each object type, including built-in types. Or you could define that all functions return on object of type 'ErrorResult' which has both the actual result value(s) and the error state as components. I'm sure there are many other possible ways. Second, 0 or 1 are not the only possible base values for an index. It might be the exception rather than the rule, but in a certain context, just about any range might make sense. You could even allow enum types to serve as an index, in which case the term 'base' actually loses its meaning, at least from the PoV of the developer. Let's say you have an array with one element for each weekday. Now, what would you consider the 'base index' of such an array, when your code looks like this:
enum weekdays { Monday, Tuesday /* ... */ };
menu[Sunday].lunch = "chicken curry";You could also allow strings or other types as an index, obliterating the boundaries between maps and arrays, but then it would be hard to determine the actual array size, not to mention implement its layout.
-
I think you are still thinking ONLY about pointers. Suppose I have a listbox and I want to loop through all of its items (elements). I can start a loop like this in VB.NET "For i=0 to ListBox1.Items.Count-1" in Zero based index. Here you can see that there is one more calculation performed (ListBox1.Items.Count-1). If the index is based on One, the loop can be transformed to "For i=1 to ListBox1.Items.Count" which is using one less calculation than Zero based (Actually, this also depends on number of items in the listbox. The loop will need to calculate "Count-1" up to the number of items in the listbox.) In common sense, if there is "nothing" it means Zero and everything starts with One. And programmers are not computers, programming languages can be designed to use starting index as Zero or One or anything most preferred. The compiled code can address anything using the zero based index but this must not be necessary for a programmer/programming language to use the same. More and more programming languages feature are added just to ease programming, otherwise programming can be done directly in Binary.
If I am reading this correctly, you are stating that VB.NET has to recalculate the value of (ListBox1.Items.Count-1) through EVERY ITERATION of the loop; does anyone else see the flaw in that logic? The trouble with your example is this: you are making the assumption that the compiler will not optimise the above code at compile time. If you can show me that VB.NET (or any modern high-level language, for that matter) won't ensure that in almost all cases (ListBox1.Items.Count-1) is precalculated and placed in a register when compiled into machine code, I'll show you a programming language that needs a serious rewrite. Another thing that compilers like to optimise for is the ability to test against 0, which often uses less code and is several cycles faster in many cases than tests against non-0 values. While most compilers will quite happily refactor a (0 to (count-1)) loop to take advantage of the faster test, a (1 to count) loop may force the compiler to use more/slower code in order to preserve the indexing, thereby wasting cycles. While this may not be true all of the time (and probably not so much now as it was when I first started coding), the point is that 0-based indexing can often provide a hint to the compiler to use a faster increment/decrement strategy. I think it's obvious now which camp I stand in. I think of it like this: if everything started at 1, then I would be a year older than I really am, and I'm old enough as it is. Bsides, when I was born I would have been 1 year old by default, which is clearly wrong no matter how bad you are at mathematics. In life as well as code, one must always start from 0 and work up from there. :-D
"All programming is an exercise in caching." - Michael Abrash, GPBB
-
If I am reading this correctly, you are stating that VB.NET has to recalculate the value of (ListBox1.Items.Count-1) through EVERY ITERATION of the loop; does anyone else see the flaw in that logic? The trouble with your example is this: you are making the assumption that the compiler will not optimise the above code at compile time. If you can show me that VB.NET (or any modern high-level language, for that matter) won't ensure that in almost all cases (ListBox1.Items.Count-1) is precalculated and placed in a register when compiled into machine code, I'll show you a programming language that needs a serious rewrite. Another thing that compilers like to optimise for is the ability to test against 0, which often uses less code and is several cycles faster in many cases than tests against non-0 values. While most compilers will quite happily refactor a (0 to (count-1)) loop to take advantage of the faster test, a (1 to count) loop may force the compiler to use more/slower code in order to preserve the indexing, thereby wasting cycles. While this may not be true all of the time (and probably not so much now as it was when I first started coding), the point is that 0-based indexing can often provide a hint to the compiler to use a faster increment/decrement strategy. I think it's obvious now which camp I stand in. I think of it like this: if everything started at 1, then I would be a year older than I really am, and I'm old enough as it is. Bsides, when I was born I would have been 1 year old by default, which is clearly wrong no matter how bad you are at mathematics. In life as well as code, one must always start from 0 and work up from there. :-D
"All programming is an exercise in caching." - Michael Abrash, GPBB
I don't know weather compiler would optimize the code of ListBox1.Items.Count-1 in For loop or not. I think compiler will not optimize the code because the Items.Count may change when executing the statements inside the loop and therefore it must be calculated whenever this statement is encountered. And about the example of life. If you compare life with programming, then you would be 1 year+9 months old at birth-time. Your age could be 1.5 year at any point of time but there is no array element with the index 1.5 in any programming language. However I am not good at maths, I know that Zero means no-thing and 1 means, there is at-least One thing. You can say that there is Zero apple on the desk when there is no apple on the desk, but doesn't it looks wrong when you say that there is Zero element in an array when the array has One element? This is like a paradox.