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

fix(complete): Use "expect" instead of "unwrap" #3322

Closed
wants to merge 1 commit into from
Closed

fix(complete): Use "expect" instead of "unwrap" #3322

wants to merge 1 commit into from

Conversation

hasezoey
Copy link

Use expect instead of unwrap to provide context in case it does fail

fixes #3319


i know that @epage said at #3319 (comment)

The reason the error message isn't better is that it isn't intended for this case. Same for why the bin_name is required

but i still think it is better to use expect instead of unwrap to get a better debugging experience in case it does fail (like accidental misuse in my case)

Use "expect" instead of "unwrap" to provide context in case it does fail
@hasezoey
Copy link
Author

updated to fix rustfmt

let bin_name = app.get_bin_name().unwrap();
let bin_name = app
.get_bin_name()
.expect("Getting the App's \"bin_name\" failed");
Copy link
Member

Choose a reason for hiding this comment

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

This reads like its describing app.get_bin_name().unwrap() and isn't adding any value.

A tip for writing expects is to use them to document what the expected pre-condition is that failed. In this case, crate::generate set the bin_name

Copy link
Author

Choose a reason for hiding this comment

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

and isn't adding any value.

i mean its still better than unwrap failed at, this way you currently have at least the clue what property is missing before having to investigate the code that threw the message

but yes, the message is not really great

how about changing the message to:
Getting the App's \"bin_name\" failed: did crate::generate set the \"bin_name\" correctly?

Copy link
Member

Choose a reason for hiding this comment

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

The distinction is expects are not error reporting but a form of assertion. Developers get a traceback telling them where it failed and can easily see what the line is that failed. The emphasis should be on the documentating the invariant (by emphasis, I mean linguistically)

This goes back to the tip I mentioned. A more concrete way of putting it is to turn "expect("...")into "expect ..." and fill in the sentence fragment. In this case "expect crate::generate set the bin_name" which becomesexpect("crate::generate set the bin_name")`. Yes, the panic message isn't optimized for this (its a common complaint of expect's panic message).

Copy link
Author

Choose a reason for hiding this comment

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

expect crate::generate set the bin_name
crate::generate set the bin_name

they dont sound descriptive to me, how about Expected crate::generate to have set "bin_name" and maybe with the addition of (Did this function get run directly?) as a hint to not run this function directly (but through crate::generate)

Copy link
Member

Choose a reason for hiding this comment

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

Expected crate::generate to have set "bin_name"

As I said, this is redundant with the line that is failing the assert

(Did this function get run directly?)

This is more of error reporting verbage than assertion violation verbage.

epage added a commit to epage/clap that referenced this pull request Jan 21, 2022
@epage epage mentioned this pull request Jan 21, 2022
@epage
Copy link
Member

epage commented Jan 21, 2022

To move this forward, I've gone ahead and created #3328

@epage epage closed this Jan 21, 2022
@epage
Copy link
Member

epage commented Jan 21, 2022

btw some of these problems will hopefully go away when we resolve #2911.

@hasezoey hasezoey deleted the hasezoey-patch-complete-expect branch January 22, 2022 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants