Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. From where should the index start?

From where should the index start?

Scheduled Pinned Locked Moved The Lounge
questionphpdatabasedesignhelp
114 Posts 31 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Nikunj_Bhatt

    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?

    S Offline
    S Offline
    Stefan_Lang
    wrote on last edited by
    #105

    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.

    1 Reply Last reply
    0
    • N Nikunj_Bhatt

      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.

      S Offline
      S Offline
      Stefan_Lang
      wrote on last edited by
      #106

      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 ;) )

      1 Reply Last reply
      0
      • N Nikunj_Bhatt

        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?

        K Offline
        K Offline
        Kabwla Phone
        wrote on last edited by
        #107

        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];

        1 Reply Last reply
        0
        • N Nikunj_Bhatt

          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)?

          A Offline
          A Offline
          Antonino Porcino
          wrote on last edited by
          #108

          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 of FOR i=0 to count-1.

          1 Reply Last reply
          0
          • N Nikunj_Bhatt

            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?

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #109

            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.

            1 Reply Last reply
            0
            • N Nikunj_Bhatt

              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.

              E Offline
              E Offline
              Eric A Carter
              wrote on last edited by
              #110

              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

              N 1 Reply Last reply
              0
              • E Eric A Carter

                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

                N Offline
                N Offline
                Nikunj_Bhatt
                wrote on last edited by
                #111

                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.

                E 1 Reply Last reply
                0
                • N Nagy Vilmos

                  Where we can, we should not be using indexes today. When we do, need to respect how things are stored.


                  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

                  N Offline
                  N Offline
                  Nikunj_Bhatt
                  wrote on last edited by
                  #112

                  You are right that

                  Nagy Vilmos wrote:

                  Where we can, we should not be using indexes today.

                  But

                  Nagy Vilmos wrote:

                  When we do, need to respect how things are stored.

                  I think, you are forgetting the question. Sentences in my first post in this thread is :

                  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?

                  In the question, I am clearly asking "if you are going to design a new language" & "forget that any programming language already exist". So, I was expecting answer while keeping these words in mind. If you can design a new language, you can also define how the things will be stored too. Otherwise nothing innovative exist in your mind; you may be following a crowd of mad people.

                  1 Reply Last reply
                  0
                  • N Nikunj_Bhatt

                    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.

                    E Offline
                    E Offline
                    Eric A Carter
                    wrote on last edited by
                    #113

                    1. I stand corrected - I just ran a few simple tests which prove your case. I still hold that in certain situations, 0-based can produce faster machine-level code than non-0; clearly this is not one of them, due to runtime recalculation concerns. 2. Technically you are correct, but unless there are some really pedantic people out there, I know of noone who would say they just turned 'X years and X weeks' old on thier birthday (depending on how many weeks they gestated for - not everyone spends 9 months in the womb). In some ways, the difference between 0 and 1 in this discussion is moot, because eventually everything comes down to 0-based ADDRESSING in machine code anyway. As far as 'indexing' (an entirely different thing to 'addressing', mind you) in a high-level language is concerned, there are 3 basic options which are entirely preference-dependant: a. 0-based indexing, mimicing addressing to nullify/trivialise index-to-address calculations; b. 1-based indexing, mimicing the human perception of quantity; c. N-based indexing, allowing the user to specify the base index to complement a particular indexing system used 'natively' by a code block. Personally, I prefer the 0-based approach, because the machine works that way for addressing anyway, and any other indexing system can be emulated (for almost no cost these days) by simply adding an offset value: 1-based = index + 1, 12-based = index + 12, -345-based = index + -345; not hard at all. If I was to write a high-level language of my own however, I admit I'd probably code in the option of preset array offsets, with a default offset of 0 if one wasn't specified at array allocation. All of these options have been used to great effect in many languages over the years, and all of them have pros and cons. If you're writing a high-level/scripting language, your best bet is to go with the indexing system that works best for you first, and if others like it, that's a bonus.

                    N 1 Reply Last reply
                    0
                    • E Eric A Carter

                      1. I stand corrected - I just ran a few simple tests which prove your case. I still hold that in certain situations, 0-based can produce faster machine-level code than non-0; clearly this is not one of them, due to runtime recalculation concerns. 2. Technically you are correct, but unless there are some really pedantic people out there, I know of noone who would say they just turned 'X years and X weeks' old on thier birthday (depending on how many weeks they gestated for - not everyone spends 9 months in the womb). In some ways, the difference between 0 and 1 in this discussion is moot, because eventually everything comes down to 0-based ADDRESSING in machine code anyway. As far as 'indexing' (an entirely different thing to 'addressing', mind you) in a high-level language is concerned, there are 3 basic options which are entirely preference-dependant: a. 0-based indexing, mimicing addressing to nullify/trivialise index-to-address calculations; b. 1-based indexing, mimicing the human perception of quantity; c. N-based indexing, allowing the user to specify the base index to complement a particular indexing system used 'natively' by a code block. Personally, I prefer the 0-based approach, because the machine works that way for addressing anyway, and any other indexing system can be emulated (for almost no cost these days) by simply adding an offset value: 1-based = index + 1, 12-based = index + 12, -345-based = index + -345; not hard at all. If I was to write a high-level language of my own however, I admit I'd probably code in the option of preset array offsets, with a default offset of 0 if one wasn't specified at array allocation. All of these options have been used to great effect in many languages over the years, and all of them have pros and cons. If you're writing a high-level/scripting language, your best bet is to go with the indexing system that works best for you first, and if others like it, that's a bonus.

                      N Offline
                      N Offline
                      Nikunj_Bhatt
                      wrote on last edited by
                      #114

                      You are right that:

                      Eric A. Carter wrote:

                      the difference between 0 and 1 in this discussion is moot, because eventually everything comes down to 0-based ADDRESSING in machine code anyway

                      From this:

                      Eric A. Carter wrote:

                      Personally, I prefer the 0-based approach, because the machine works that way for addressing anyway

                      :doh: :( :(( I have always found in this thread that most of all are thinking about computer/compiler (machine) and not about programmer (human). Are we (programmer, humans) making code for sake of machine? Are the variety of languages made to ease compilers or to ease programming for programmers (humans)? And I don't think everyone is writing their thoughts after thinking if they were to build a new language while keeping in mind that there is no programming language exist.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups