Based on a common function signature pattern that we have all around the sources:
0bool doSomething(arg1, arg2, arg3, arg4, &result_obj, &error_string) {
1 // do something...
2 if (error) {
3 error_string = "something bad happened";
4 return false;
5 }
6
7 result = goodResult;
8 return true;
9}
Introduced a generic class BResult
that encapsulate the function boolean result, the result object (in case of having it) and, in case of failure, the string error reason.
Obtaining in this way cleaner function signatures and removing boilerplate code:
0BResult<Obj> doSomething(arg1, arg2, arg3, arg4) {
1 // do something...
2 if (error) return "something bad happened";
3
4 return goodResult;
5}
Same cleanup applies equally to the function callers’ side as well. There is no longer need to add the error string and the result object declarations before calling the function:
Before:
0Obj result_obj;
1std::string error_string;
2if (!doSomething(arg1, arg2, arg3, arg4, result_obj, error_string)) {
3 LogPrintf("Error: %s", error_string);
4}
5return result_obj;
Now:
0BResult<Obj> op_res = doSomething(arg1, arg2, arg3, arg4);
1if (!op_res) {
2 LogPrintf("Error: %s", op_res.GetError());
3}
4return op_res.GetObjResult();
Initial Implementation:
Have connected this new concept to two different flows for now:
- The
CreateTransaction
flow. –> 7ba2b87c - The
GetNewDestination
flow. –> bcee0912
Happy note: even when introduced a new class into the sources, the amount of lines removed is almost equal to added ones :).
Extra note: this work is an extended version (and a decoupling) of the work that is inside #24845 (which does not contain the GetNewDestination
changes nor the inclusion of the FeeCalculation
field inside CreatedTransactionResult
).