Skip to content

binary-search: Is searching for a nonexistent item an error? #1312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
petertseng opened this issue Aug 31, 2018 · 3 comments
Closed

binary-search: Is searching for a nonexistent item an error? #1312

petertseng opened this issue Aug 31, 2018 · 3 comments

Comments

@petertseng
Copy link
Member

Notice that https://github.com/exercism/problem-specifications/blob/master/exercises/binary-search/canonical-data.json uses -1 to indicate "searched-for item is not in the array". The question: Should this be considered an error? If so, binary-search is covered by #1311 .

I consider searching for a nonexistent item an occurrence that the implementation should expect to routinely happen -- in a language with exceptions, for example, I would not recommend the use of an exception for this. A language with optionals would use its None for this case, and use Some(index) when there actually is an index.

However, I'll refrain from commenting on whether this should be represented in JSON as a -1 sentinel or an error object or null.

In the absence of any other preference, I would suggest to keep it as -1, and close this issue without changes.

@Insti
Copy link
Contributor

Insti commented Aug 31, 2018

I agree it is not an error.

I would suggest to keep it as -1, and close this issue without changes.

I'm fine with this.

@NobbZ
Copy link
Member

NobbZ commented Aug 31, 2018

I do not think this were an error, as it just tells that we do not have an item there.

I do not like it beeing -1, this reminds me pretty much of C or PHP. There are a lot of languages that have different idioms around this.

I'd prefer specifiying null as expectation in the JSON and let the tracks deal with their idioms and how it is representable.

If there is a literal -1 in the expectation, people will tend to mechanically copy and paste it into their testsuites.

@coriolinus
Copy link
Member

coriolinus commented Aug 31, 2018

The correct answer to this question is strongly dependent on the idoms of language being used. Some off the top of my head:

  • C, PHP: -1
  • go: 0, false tuple
  • rust: None from the Option enum
  • Haskell: Nothing from the Maybe monad
  • python: KeyError exception or None
  • bash: ""

The problem, to me, is that it is possible to return -1 to indicate this condition in all of these languages, but it is unidiomatic in the majority of them. If we use a non-error -1 to indicate this condition in the canonical data, it can be interpreted as a requirement that languages implement that, despite it being unidiomatic.

For that reason, I prefer to represent it as an error; this more clearly signals to track maintainers that special handling and rewriting may be required. This doesn't preclude tracks from using a magic value in languages where that is idiomatic, and it allows tracks with better error handling than magic values to proceed with their native idioms.

petertseng added a commit that referenced this issue Sep 24, 2018
binary-search 1.2.0

As discussed in
#1312

Although -1 is a sentinel value, using this sentinel value is not the
usual course of action in some languages. In using an error object, we
avoid giving the wrong idea that we are requiring the use of the
sentinel value.

This error value was defined in
#401

Of course, languages that wish to use a sentinel value may continue to
do so; this commit is not intended to decree that sentinel values are
forbidden.

Neither is this commit decreeing that all languages must represent this
condition as an error; it is simply a declaration that this condition
bears enough consideration that we'll represent it with a different
type.

Closes #1312
Checks the related box in #1311
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants