-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
Use "expect" instead of "unwrap" to provide context in case it does fail
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"); |
There was a problem hiding this comment.
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 expect
s is to use them to document what the expected pre-condition is that failed. In this case, crate::generate set the bin_name
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 becomes
expect("crate::generate set the bin_name")`. Yes, the panic message isn't optimized for this (its a common complaint of expect's panic message).
There was a problem hiding this comment.
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
)
There was a problem hiding this comment.
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.
This was inspiredby clap-rs#3322
To move this forward, I've gone ahead and created #3328 |
btw some of these problems will hopefully go away when we resolve #2911. |
Use
expect
instead ofunwrap
to provide context in case it does failfixes #3319
i know that @epage said at #3319 (comment)
but i still think it is better to use
expect
instead ofunwrap
to get a better debugging experience in case it does fail (like accidental misuse in my case)