3 ms·
Jeez. I proposed to adopt some of the Joint Strike Fighter Air Vehicle rules for C++ [1] at one job, and I went through them marking each as "already in practic
by ReactiveJelly 3y ago
Jeez. I proposed to adopt some of the Joint Strike Fighter Air Vehicle rules for C++ [1] at one job, and I went through them marking each as "already in practice", "needs debate" or "I'm vetoing it"
And I vetoed early return with a link to the article on railroad error handling. I don't know how it looks in C, but in C++ and Rust early returns are so much better.
[1] https://www.stroustrup.com/JSF-AV-rules.pdf https://www.stroustrup.com/JSF-AV-rules.pdf
- paxcoder 3y agoI deprecate early returns (at least for happy cases). What article are you referring to?
- duped 3y agoEvery time I write an early return (which is every time) I think about my first college lab that had the rules "no global variables, one return statement per function" that if you violated you were failed for the lab. Luckily our TAs graded those and they cared about as much about those rules as everyone else. Today one is a principle at MS and the other at Google. Guess they knew how to code.
- acuozzo 3y agoNo RAII means that allowing early returns in C can increase the likelihood of not performing cleanup along error paths. Consider writing a function called connect() which opens a socket and, if successful, calls setsockopt, bind, and friends. If e.g. bind fails and you choose to return early, then you also have to remember to close the opened socket. In my own code I prefer to use early returns for all situations except ones like the aforementioned. For those I use a forward goto to a label named "off_ramp" which does the necessary cleanup and returns an error code.