5 ms·
It seems like the author's main complaints are avoided if one follows the Google C++ style guide, which says: - Don't use C++ exceptions - Don't do work in co
by sxyuan 6y ago
It seems like the author's main complaints are avoided if one follows the Google C++ style guide, which says:
- Don't use C++ exceptions
- Don't do work in constructors (prefer "Init" or factory functions instead)
https://google.github.io/styleguide/cppguide.html https://google.github.io/styleguide/cppguide.html
There may be other reasons to prefer C over C++, but if you don't like exceptions, you don't have to use them.
- hedora 6y agoRegardless of what the style guide says, multiple studies of real-world exception handling correctness have been done, and the results are clear: Very few (if any) real-world codebases correctly handle exceptions. As the article points out, correct exception handlers would grow exponentially with program size. People can’t reason about that, so they don’t handle things like multiple error paths, or they conflate one error code with another, and so on. The results hold across programming languages.
- Ididntdothis 6y ago“prefer "Init" or factory functions instead“ I vote for factory functions. They bring you pretty close to how C would do it.
- gumby 6y agoThe google style guide says “ On their face, the benefits of using exceptions outweigh the costs, especially in new projects. However, for existing code...” and they use a lot of existing code.
- pansa2 6y ago> they use a lot of existing code. Doesn’t everyone? Lots of C++ code is not exception safe, including large libraries such as Qt. If I’m writing code that uses such a library, would it be better to take Google’s approach and avoid using exceptions myself, or try to “wrap” the non-exception-safe library somehow?
- gumby 6y agoThere is no single answer to that — depends on the library and how you use it. But if it’s not exception safe it’s not exception safe. There is no way to tell if a constructor failed without catching an exception.
- rumanator 6y ago> Don't use C++ exceptions That's not what Google's C++ style guide says at all. They only argue that Google's old C++ code base is not exception-tolerant, thus as they don't want to waste time and they don't want risk adding bugs by refactoring their legacy code then they just decided to not use exceptions. That's it.
- sxyuan 6y agoYou're right. My point wasn't that one should blindly follow Google's style guide and avoid using C++ exceptions, only that there is an alternative to the C/C++ dichotomy that the author presents. It's possible to write millions of lines of C++ without using exceptions. There's a trade-off, which is described in more detail in the style guide, and the choice depends on the specifics of one's project.