-
Notifications
You must be signed in to change notification settings - Fork 2.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
Add available macros for 'return error code' as an alterative to 'verifyorexit' #3880
Add available macros for 'return error code' as an alterative to 'verifyorexit' #3880
Conversation
Convert a few more rendezvous to return macros instead of verify - seems 0 code size difference
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.
yay! on our way to no labels?
I am not a fan of |
|
||
if (endPoint != nullptr) | ||
{ | ||
err = endPoint->Send(msgBuf); | ||
return endPoint->Send(autofree.Release()); | ||
} | ||
else |
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.
What's our general position on else after return?
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.
I generally prefer single point of return or exit / error handling (RAII or not) which moots this particular facet of coding style discussion.
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.
I have created this PR after seeing that mandating goto with single point of exit can result in significantly hard to follow code.
The premise is that we will have the option to use return if the code is cleaner as a result. Mandating exit label always seems too strong of a requirement.
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.
Regarding the else after return: I think generally I would say no else is needed after return. In this particular case however, this is a final statement that reads like:
return (endpoint != nullptr)
? endPoint->Send(autofree.Release())
: SendAfterConnect(address, autofree.Release());
so I thought else helps clarity. I would drop the else if the readability reviews would ask for it.
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.
I support @gerickson. Probably it's a matter of taste and it will be hard to decide on "the right" approach. For me this change makes code less readable and more error-prone (especially if we'll start mixing e.g. VerifyOrExit
with VerifyOrReturn
in a single method). But maybe it's just my embedded background..
Of course giving the choice for developers is good, though we lose consistency when both approaches are used in different places.
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.
+1 that mixing in a single method would be absolutely bad - you get the benefit of neither. I am not arguing for that. I am arguing that I should be allowed to use return if it makes the code easier/clearer to read.
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.
I could not find a good way to count, but find . -type f | egrep -v '/third_party/' | xargs egrep -zc 'exit:\s*return' | grep -v ':0' | wc -l
seems to indicate that there are 149 instances today where 'exit: return...' is used. I believe those instances would benefit from just using return in the first place.
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.
C forces you into the goto exit pattern, but C++ doesn't, and the goto style with declarations front loaded seems unusual for C++. Google's style guide doesn't even bother to recommend against using goto because it is so uncommon in the first place [1].
Size increase report for "nrfconnect-example-build" from 188a35f
Full report output
|
Size increase report for "esp32-example-build" from 188a35f
Full report output
|
|
||
if (endPoint != nullptr) | ||
{ | ||
err = endPoint->Send(msgBuf); | ||
return endPoint->Send(autofree.Release()); | ||
} | ||
else |
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.
C forces you into the goto exit pattern, but C++ doesn't, and the goto style with declarations front loaded seems unusual for C++. Google's style guide doesn't even bother to recommend against using goto because it is so uncommon in the first place [1].
Problem
Existing coding pattern of 'exit:' goto label is convenient for cleanup code, however it encourages C-style coding which has drawbacks:
Summary of Changes
Creates Verify/return macros that are equivalent to the 'exit' macros and converts some files with sample usage for these macros.
Generated code is generally the same size as the previous goto code (with some slight immaterial improvements) and source code is shorter.