I’m currently writing a CLI tool that handles a specific JSON data format. And I also want to give the user to get a slice of the item array of the file. It’s a slice in form of --slice START:END through commandline options. So in example --slice 1:2.

  1. Should I provide a 0 based index for the access or a 1 based index? In example --slice 1:2 with 0 based index would start with the second element and with 1 based index it would start with the first element.
  2. And would you think its better to have the END to be inclusive or exclusive? In example --slice 1:2 would get only one element if its exclusive or it gets two elements if its inclusive.

I know this is all personal taste, but I’m currently just torn between all options and cannot decide. And thought to ask you what you think. Maybe that helps me sorting my own thoughts a bit. Thanks in advance.

  • atomic peach@pawb.social
    link
    fedilink
    English
    arrow-up
    2
    ·
    2 days ago

    How much time do you have to program in edge cases? If I had the time and it were me, I’d parse for single number and return a single element. A single number with colon would give the front/back portion of the list accordingly. And two numbers gives the inclusive range.

    Then in terms of if you get a zero, swap to zero index mode since they clearly want the start of the list or reject the command explaining the argument isn’t zero-based (probably best to reject just for consistency).

    The docs/help page will be key here. That and consistency across your app when it comes to zero vs one indexing.

    • thingsiplay@beehaw.orgOP
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      2 days ago

      I think that I’m going with these approaches. For the ‘0’, I’m now accepting it as the 0 element. Which is not 0 based index, but it really means before the first element. So any slice with an END of 0 is always nothing. Anything that starts at 0 will basically give you as many elements as END points to.

      • 0: is equivalent to : and 1: (meaning everything)
      • 0 is equivalent to 0:0 and :0 (meaning empty)
      • 1:0 still empty, because it starts after it ended, which reads like “start by 1, give me 0 elements”
      • 1:1 gives one element, the first, which reads like “start by 1, give me 1 element”

      I feel confident about this solution. And thanks for everyone here, this was really what I needed. After trying it out in the test data I have, I personally like this model. This isn’t anything surprising, right?

      • locuester@lemmy.zip
        link
        fedilink
        English
        arrow-up
        1
        ·
        edit-2
        15 hours ago

        I personally find it easier for non programmers to use a START:LENGTH model.

        3:5 is (up to) 5 elements starting at the third.

        1:1 is just the first element

        Any 0 is invalid

        20:2 is elements 20 and 21

        It eliminates inclusive/exclusive questions.