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.

  • K2yfi@programming.dev
    link
    fedilink
    arrow-up
    6
    ·
    1 month ago

    I’ve been working on this problem for my own language, and have landed on something more clear than just following a convention. Basically you use [] and () to specify if the left and right bounds are included or not (based off of interval notation: https://en.wikipedia.org/wiki/Interval_(mathematics)#Including_or_excluding_endpoints). e.g. for your case

    --slice [1:5)    # include the left index. don't include the right index
    --slice [1:5]    # include both left and right index
    --slice (1:5]    # don't include the left index. include the right index
    --slice (1:5)    # don't include the left or right index
    

    potentially not relevant to your case, but my version supports an end keyword which you can do math on, similar to python’s negative indexing

    [2:end-3]    # start at index 2 (included) and go through till the third from last index (included)
    (end-3:end]  # start at the third from last (excluded) and go to the end (included)
    

    Personally I’m a fan of 0 indexing, but for your context, I think it would depend on how the user sees what they’re slicing. E.g. if it was pages with page numbers, the numbers would indicate if it was 0 or 1 index based. If there’s nothing to actually show the user, I think picking something reasonable and documenting it well is probably the best bet.

    • thingsiplay@beehaw.orgOP
      link
      fedilink
      arrow-up
      1
      ·
      5 days ago

      Sorry for late reply. I actually like the idea to use words to indicate start and end as well. But with additional “math like” features it gets a little bit complicated to parse for simple slicing as an option in a program. It makes much more sense to do this flexible thing in a language. BTW its shouldn’t be needed to have an end if you already have a negative number.

      I use the empty string as indicator for start or end "..2" (BTW I switched to .. separator after long thinking, reading arguments and debating with myself).

      Just as an idea, you could provide another variable mid so one could start counting from there (mid+2:end). And man the idea sounds really elegant to use [ or ( to indicate if the number is inclusive or exclusive. But I fear it could be a little bit confusing and people could forget which one is which. I would rather prefer special characters that are not used otherwise. In example both sides are always inclusive and there is a special character before or after the number to indicate its exclusion. A character that is not used otherwise in numbers or slices and is representing the exclusion unmistakably, and I have absolute no idea what this could be.

      I have to say the usage of brackets to indicate its inclusion or exclusion is creative thinking!