Google C++ Style Guide
google-styleguide.googlecode.com
google-styleguide.googlecode.com
Thus, here's the C++ Frequently Questioned Answers List:
do_something or die "Somebody set up us the bomb"Easily doable with handler-case, unwind-protect and reader macros, but ugly as sin.
I have always felt that C++ is a language too complicated in a time where keeping it simple is key. The fact that it needs a style guide that long says it all.
I also think C++ is too complicated, but it's an amazing powerful language, if you master c++ you can pretty much master any language out there.
Deep knowledge of C++ is something I would love to have, and I have much respect for those who can grasp all of its concepts.
For instance every non-trivial language with more than a couple of programmers working with it will have some style (maybe less formal) style guide. Python is often hailed as 'executable sudo-code' yet the community has a style guide, pep8. (Interestingly Google has their own style guide for Python.)
There are plenty of valid reasons to hate on C++, but being a cowboy coder isn't really a valid one.
There's actually 2 Hacker News posts already linking to this article, so you can read the comments are posted there as well: http://news.ycombinator.com/item?id=885482 http://news.ycombinator.com/item?id=2220917
Of course if you are simply building forts out of sofa cushions then you don't need all this.
http://google-styleguide.googlecode.com/svn/trunk/google-c-s...
That's another funny one...
(FYI: I'm talking about VCS-guards, who prevent checkin's, if they do not comply with a style guide. Maybe someone can run a poll on that topic.)
Automated rules built into VCS seemed to be the kind of thing nobody there would tolerate.
const int kDaysInAWeek = 7; const CFAllocatorRef kCFAllocatorDefault;
const CFAllocatorRef kCFAllocatorSystemDefault;
const CFAllocatorRef kCFAllocatorMalloc;
const CFAllocatorRef kCFAllocatorMallocZone;
const CFAllocatorRef kCFAllocatorNull;
const CFAllocatorRef kCFAllocatorUseContext;Combining those two, you have code that wasn't written exception defensively having unexpected exit paths due to exceptions and now you've got bugs.
It sounds like they would've included exceptions if they didn't have a legacy codebase. I'm sure that would've added a decent amount to the doc.
try { throw 5; } catch (const int& val) { printf("%d\n", val); }
try { throw "hello_world"; } catch (const char *&val) { puts(val); }
I think it would have been much better (and simpler for implementers!) if they had made std::exception a more fundamental part of the language and required that throw be used on a descendant of it. As it stands if you want to be sure to catch any failure from some external code you end up doing: try {
something();
} catch (const std::exception& ex) {
fprintf(stderr, "ERROR: %s\n", ex.what());
} catch (...) {
fputs("UNKNOWN ERROR\n", stderr);
}
My personal opinion is that it's easier to deal with C++ exceptions (warts and all) than to try to avoid them. Just about any method that mutates a STL type can have a heap allocation fail and the only reasonable way to report that is via "throw std::bad_alloc();". Just make sure that your code always follows the principals of RAII ( http://en.wikipedia.org/wiki/RAII ) and you'll be fine.It's as if every time a machine broke down in a factory, the workmen would have to wheel the machine into the manager's office, shove it on his desk and shout, "there, there's your problem!"... and if the manager didn't grab the machine, it would fly through his back window...
As they say that the end "Use common sense and BE CONSISTENT." http://google-styleguide.googlecode.com/svn/trunk/cppguide.x... If you are modifying a project that uses exceptions, then use exceptions. If you then modify a different project that uses error codes instead, then use error codes. Be consistent above all else.
1) It means that you have to check the return value of every call. Miss one, and errors get ignored, which can result memory corruption or worse.
2) Checking return codes after every function call results in terribly verbose code/ Whereas exception-based error handling, when used in the right way, only needs catch() statements in places where the error is actually handled.
2) Every function has to return a status code. This hijacks the return value, meaning that you cannot simply return a value anymore, you always need to use output parameters.