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.
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.
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.
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.