Skip to content

Use expect instead of unwrap for calls to consensus_encode#719

Merged
RCasatta merged 4 commits intorust-bitcoin:masterfrom
tcharding:no-err-writes-use-expect
Dec 1, 2021
Merged

Use expect instead of unwrap for calls to consensus_encode#719
RCasatta merged 4 commits intorust-bitcoin:masterfrom
tcharding:no-err-writes-use-expect

Conversation

@tcharding
Copy link
Copy Markdown
Member

Calls to unwrap outside of tests are generally unfavourable. We currently call unwrap in a bunch of places on calls to consensus_encode when passing writers that do not fail.

Remove unwrap calls on all calls to consensus_encode that pass a writer argument for which write functions do not fail. Use expect with a descriptive string instead.

Fixes: #714

In the name of uniformity use the same error message as argument to
`expect` througout the codebase.

Use "engines don't error" instead of "engines don't err".
Calls to `unwrap` outside of tests are typically unfavourable.

Hash engines do not error when calling `consensus_encode`. Instead of
the current usage of `unwrap` we can use `expect` with a descriptive
string as is done in other parts of the codebase.
Calls to `unwrap` outside of tests are typically unfavourable.

Sink writers do not error. We can use `expect` with a descriptive
message string to indicate this.
Calls to `unwrap` outside of tests are typically unfavourable.

In memory writers (`Vec`) do not error. We can use `expect` with a
descriptive message string to indicate this.
@dr-orlovsky
Copy link
Copy Markdown
Collaborator

I think we need this change; the only question is how to write the message in expects. This message is print in STDERR in form main thread panicked at ... with error: {expect_message} {error_message}

So I think expect messages should be written in form saying what happened and not what cant't happen: memory writer failure instead of memory writers doesn't fail etc

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Nov 25, 2021

@dr-orlovsky it's not as straightforward. I commonly use the message instead of a comment explaining why it's OK to panic be cause I expect it to never happen. (Although once I did actually experience a funny message "std command API is retarded".) If the message is not explaining why it shouldn't happen then there needs to be a comment.

@tcharding
Copy link
Copy Markdown
Member Author

I used to write the expect message so it was meaningful during the panic but that makes the reading of the code worse because everytime one reads the error they think 'why do we panic here if this can fail?'. Using a message written for the code reader makes reading the code easier (which is done often) and deciphering the panic harder (which should not be done often). Hence I favour writing the expect message for the code reader "engines don't error".

Copy link
Copy Markdown
Member

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

LGTM, but please avoid spending too much time on this kind of thing. These types of trivial changes in unreachable code, or other trivial code motion that doesn't change user-experience and doesn't change feature sets are largely responsible for the standstill in major improvements that has plagued the Bitcoin Core project for years - lets not extend it to rust-bitcoin please.

Trivial fixes are great when they're treated as trivial and merged quickly - when it ends up with reviewers trying to debate nuance of the Most Correct version of a trivial fix it can suck forward progress out of a project.

@tcharding
Copy link
Copy Markdown
Member Author

LGTM, but please avoid spending too much time on this kind of thing. These types of trivial changes in unreachable code, or other trivial code motion that doesn't change user-experience and doesn't change feature sets are largely responsible for the standstill in major improvements that has plagued the Bitcoin Core project for years - lets not extend it to rust-bitcoin please.

Point taken, I'll try to keep this in mind.

Trivial fixes are great when they're treated as trivial and merged quickly - when it ends up with reviewers trying to debate nuance of the Most Correct version of a trivial fix it can suck forward progress out of a project.

I have tendency to do a bunch of theses trivial patches especially when first entering a project, to get a feel for the project and to set a few rules down for myself going forward. Perhaps I'll preface the description of such PRs with "Please ack and merge or nack and close, this is trivial work not worth debating"?

Please do keep coming at me with knowledge like this @TheBlueMatt, the better I can understand how to help rust-bitcoin progress the better for the project :)

Cheers,
Tobin.

@TheBlueMatt
Copy link
Copy Markdown
Member

I have tendency to do a bunch of theses trivial patches especially when first entering a project, to get a feel for the project and to set a few rules down for myself going forward. Perhaps I'll preface the description of such PRs with "Please ack and merge or nack and close, this is trivial work not worth debating"?

That's totally fine! The second half of my statement was less directed at you and more other reviewers! :)

Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK a2efafc

Copy link
Copy Markdown
Collaborator

@RCasatta RCasatta left a comment

Choose a reason for hiding this comment

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

ACK e7b84e2

I wanted to merge, but using the github-merge.py tool resut "Top commit has no ACKs!".
This clearly has 2 ACKs, because @TheBlueMatt approved and @Kixunil acked the bottom commit instead of the top one, but I am not sure we want to enforce the two acks to be visible in the merge commit so I momentarily suspended the merge

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Dec 1, 2021

Sorry for confusion.

ACK e7b84e2

@RCasatta RCasatta merged commit 51b1abd into rust-bitcoin:master Dec 1, 2021
@tcharding tcharding deleted the no-err-writes-use-expect branch December 3, 2021 03:12
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.

consensus_encode uses unwrap

5 participants