Skip to content
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

Histogram error handling #25

Merged

Conversation

LukeMathWalker
Copy link
Member

Following on #16, it handles all current panic scenarios for our bin strategies, returning an Option when a suitable bin collection cannot be generated.

@LukeMathWalker LukeMathWalker mentioned this pull request Feb 2, 2019
17 tasks
/// **Panics** if `bin_width<=0`.
fn new(bin_width: T, min: T, max: T) -> Self
/// Returns `None` if `bin_width<=0` or `min` >= `max`.
fn new(bin_width: T, min: T, max: T) -> Option<Self>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: is returning no object really a valid outcome or should it actually return a Result<Self, WhateverError> to signify a failure?

Hi, curious/lurking reviewer here. Saw the first "adventure" blog post and ended up here. Really great work that you people are doing and an inspiring article/tutorial.

If returning None is genuinely a valid, non-exceptional outcome, given certain args, then maybe a function name other than new would help clarify it's behavior? Just a suggestion.

Copy link
Member Author

@LukeMathWalker LukeMathWalker Mar 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Result is indeed a better return type there - I've been lazy because EquiSpaced is not part of the public API, but that's not a reason to be sloppy.

The creation method for the public strategies is called from_array: do you think an Option is a good output type or would rather see a Result as a user?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally understand 👍

As for the public from_array I'm hesitant to give a strong opinion yet, because I've only just started looking through the code. But the general rule of thumb I would apply is this:

  • Use an Option if returning None means it's ok to continue
  • If it's not ok to continue, the user may only see a problem later down the line, when a "symptomatic" error occurs rather than the actual cause.
  • With None that symptom may just be no-output.

Is that any help?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It totally is - going again through the code I think it makes sense to convert the output type to Result. I'll work on it 👍
Thanks for your input @munckymagik!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored to return Result - let me know what you think @munckymagik @jturner314

Copy link
Contributor

@munckymagik munckymagik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LukeMathWalker it's looking good 👍 . Sorry for the delay in reviewing. Have left a few comments for you to consider.

My overall gut feeling is we should turn StrategyError into something like ShapeError in ndarray. We could use our own ErrorKind enum to represent cases like constant or empty array errors.

It would provide explicit feedback to users should they want it, and give future committers an easy path to fall-into to add new errors without breaking changes.

src/histogram/strategies.rs Outdated Show resolved Hide resolved
src/histogram/strategies.rs Outdated Show resolved Hide resolved
src/histogram/grid.rs Outdated Show resolved Hide resolved
@jturner314
Copy link
Member

I've proposed some changes in LukeMathWalker#4. In #24, we decided to use Result<Option<_>, _> in some places for consistency with methods that return None for empty input arrays. After seeing how that approach affected this PR, though, I think we should use Result<_, _> instead after all. Doing so simplifies the implementation and public API. There really isn't much disadvantage to returning Result even for things like .mean() (which now returns Result<A, EmptyInput> instead of Option<A>). There should be no performance cost, and it's easy to convert Result<T, E> to Option<T> if needed with the .ok() method on Result. I think I've also addressed all of @munckymagik's comments.

jturner314 and others added 5 commits March 31, 2019 21:36
This is nice because it doesn't lose information. (Returning an
`Option` combines the two error variants into a single case.)
Once the `#[non_exhaustive]` attribute is stable, we should replace
the hidden enum variant with that attribute on the enum.
@LukeMathWalker
Copy link
Member Author

Looking at the code in LukeMathWalker#4 after the shift to Result everywhere I do agree it looks much cleaner (and less nested thanks to ? and error conversions)!

@munckymagik
Copy link
Contributor

@jturner314 @LukeMathWalker looking really good.

There was just one other comment I made about | vs ||.

Otherwise 🚢 it.

@LukeMathWalker
Copy link
Member Author

Taken care of that one @munckymagik 👍

Copy link
Member

@jturner314 jturner314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ready to merge!

@LukeMathWalker LukeMathWalker merged commit 86e5ca4 into rust-ndarray:master Apr 2, 2019
@LukeMathWalker LukeMathWalker deleted the histogram-error-handling branch April 2, 2019 06:55
@LukeMathWalker LukeMathWalker mentioned this pull request Apr 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants