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
.
- 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. - 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.
Anybody capable of using a CLI knows that the right answer is:
Dijkstra points out why: https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html
I agree with that other comment which argues to set it as the users expect. I think the 1 based is logical here
Came here to post this. You need a very good reason to break with Dijkstra
But contrary to that, often ‘0’ is also used as the last element or points to “the entire match” in example. Whatever that is. I feel like outside of programming languages, for the end user, its not that clear of an answer. Why I created this topic.
I’ll read the linked article and rethink this topic. Maybe introducing another option to make the index 0 based (or the other way 1 based).
Where? I’ve literally never heard of this convention.
RegExes. For instance, in JavaScript,
'foobar'.match(/(foo)(bar)/)
is['foobar', 'foo', 'bar']
Now that you ask, I don’t have any example of this. I know program
head
has negative numbers to access from the last element backwardsls -1 | head -n -1
, but it does not start by 0. So yeah, the 0 as last element might be not as common as I thought to be.-1 is common. I’ve at least seen it from python.