Skip to content

poker: also using Result for the tested function#970

Closed
elektronaut0815 wants to merge 2 commits intoexercism:masterfrom
elektronaut0815:poker-result_not_option
Closed

poker: also using Result for the tested function#970
elektronaut0815 wants to merge 2 commits intoexercism:masterfrom
elektronaut0815:poker-result_not_option

Conversation

@elektronaut0815
Copy link
Copy Markdown
Contributor

I think it's more consistent if Result is used everywhere.

Copy link
Copy Markdown
Member

@coriolinus coriolinus left a comment

Choose a reason for hiding this comment

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

Thank you for doing this work, but I do not intend to merge this PR unless you can persuade me to change my mind. Rationale:

  • Option<Vec<_>> is already questionably idiomatic; an empty vector conveys precisely the same information, namely that there are no winning hands provided. Result<Vec<_>, _> doesn't provide any more information than that, because there's still only one circumstance that would trigger the Err(_) case; it's just more verbose.

  • Result<_, &'static str> is an antipattern: if there's only one way a function can fail, it's usually better to return an Option, and if there are multiple, then pub enum Error is more powerful and flexible. It's fine in things like the example, which most students don't see, but we should absolutely not put that into the public interface that every student must implement.

  • assert!(whatever().is_err()); is an antipattern: it's always better to use assert_eq!(whatever().unwrap_err(), Error::ExpectedError);

    (assert!(whatever().is_none()); is fine, though, because we're not throwing away information about which error has been raised in this case.)

I will basically always approve PRs which only add new tests to the suite, so if you wish to update the new cases to the existing API and discard the rest of the changes, we can get this merged. Otherwise, recommend closing this PR. If there is no further action, then I will close this at some point later than 1 week from now.

@elektronaut0815
Copy link
Copy Markdown
Contributor Author

Thank you for the comment. Sounds reasonable and is good advice for me on my Rust journey.
Speaking of antipatterns: Result<_, &'static str> can be found in nucleotide-codons, assert!(whatever().is_err()); also there and in grep and in rna-transcription.

@elektronaut0815 elektronaut0815 deleted the poker-result_not_option branch September 29, 2020 21:27
@coriolinus
Copy link
Copy Markdown
Member

coriolinus commented Oct 1, 2020

Following up on the antipattern discussion:

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

Successfully merging this pull request may close these issues.

2 participants