15
submitted 3 days ago* (last edited 3 days ago) by thingsiplay@beehaw.org to c/programming@programming.dev

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.

you are viewing a single comment's thread
view the rest of the comments
[-] bleistift2@sopuli.xyz 11 points 3 days ago

Anybody capable of using a CLI knows that the right answer is:

  • index from 0
  • end is exclusive.

Dijkstra points out why: https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html

[-] brisk@aussie.zone 1 points 2 days ago

Came here to post this. You need a very good reason to break with Dijkstra

load more comments (6 replies)
this post was submitted on 28 Jul 2025
15 points (100.0% liked)

Programming

21880 readers
335 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS