-
Notifications
You must be signed in to change notification settings - Fork 41
Description
In another project I've worked on, it was team policy to always return a Result and never unwrap(), expect(), panic!(), etc that abort the process.
This is a little harder to do at first, but forces one to consider and handle all cases when writing code. Rather than panicking, errors can be propagated up the call stack with ?, usually to the app layer, where it makes sense to handle them -- log, notify user, etc. The resulting code tends to be more correct and rigorous.
Further if every function is initially written to return a Result, then we don't end up with a situation where a bunch of higher level code has to be re-written when a low-level method is fixed to return a Result where it previously was calling unwrap or friends. It's sort of a "longer we wait, the harder it gets" situation.
I decided to create this issue because I am presently looking at one such case for another issue. Here Node::new() should be modified to return a Result instead of calling expect. Or alternatively it could obtain the already-parsed genesis block statements as a parameter.
Given the serious and ambitious task that Kindelia is undertaking to handle the world's money and contracts, I would suggest that this would be a good policy to adopt for future PR reviews. I believe there are some clippy lints that could eventually be turned on as well.
Here is an article that talks a bit about it.
I understand this project is moving fast and probably no one wants to take the time/effort to review and update existing code even if you agree in principle.
As such, I am willing to take on that task and begin retrofitting existing code to use more Error types and return Results. I think its a chore someone like myself without deep knowledge of the system can do, and maybe learn in the process.
I believe/hope that this work can be done in a series of small PRs rather than one huge thing. Time will tell. Another (final?) piece of the work would be adding lint checks into the CI process.
Some quick/dirty numbers for unwrap/expect/panic occurrences:
$ cd kindelia
$ find . -name \*.rs | grep -v test | grep -v bench | xargs grep unwrap | wc -l
111
$ find . -name \*.rs | grep -v test | grep -v bench | xargs grep expect | wc -l
50
$ find . -name \*.rs | grep -v test | grep -v bench | xargs grep panic | wc -l
35
Hopefully I'm preaching to the converted here and not saying anything controversial, but please let me know your thoughts.