On Fri, 05 Dec 2014 12:20:47 +0000, Chicken Mcnuggets wrote:
> I've kinda been following the Google C++ style guide (well not all of it
> but I thought it would be a good foundation to build on) and they
> recommend two things that I wanted to clarify.
>
>
> The first is never to do complex work in the constructor and instead to
> use an init() method to do all the work. This allows you to return C
> style error codes using an int to specify exactly what error occurred so
> that the caller can respond in an appropriate way.
>
> The down side of this is that calling an init() function is ugly and is
> just one more thing for the caller to remember. I'm not sure how best to
> get around that ugliness.
The payoff of an init() method is when you have multiple constructors
defined that all must share common processing. Nothing wrong with an init
() method as long as it doesn't become part of your religion.
> This is mainly in the style guide because Google forbid the use of
> exceptions in their code and therefore there is no way to signal an
> error in a constructor that does heavy work that can fail. This leads on
> to my second question.
>
> The second is never to use exceptions. This is the one I'm most thinking
> about. I recently got More Effective C++ and item 15 was talking about
> the costs of using exceptions.
I guess I'll add that to my ever growing list of why I mistrust google.
Nothing wrong with using exceptions. They area wonderful secondary
program flow mechanism. In fact I think it preferable to use them to
determine error conditions when trying to create an object, instead of
the old error return code methodology.
At the risk of starting a flame war, I think the "cost of exceptions"
argument is promulgated by old-farts who don't know or understand
exceptions. Granted, there was a time when the minimal memory and cpu
power of embedded systems made is costly to used exceptions in code, but
(especially in embedded systems), I prefer the higher level of process
abstraction and OO methodology to help me design solid code. I once
worked on a medical device using vxWorks and a special internal "team"
had designed a development C++ toolkit around vxWorks that we were to use
in making our control code. This toolkit displayed a lot of those legacy
phobias because the designers were old C programmers, not OO guys. So in
the end our code was buggier and harder to validate...and I myself am an
old-fart, but not afraid to learn new stuff.
> My code may not be that great but as I learn this code is going to be
> used in an environment that has to deal with numerous simultaneous TCP
> network connections without breaking a sweat. I'm concerned that if I
> use exceptions from the start that my code will suffer later on when I
> need to put it in a high load environment. I don't want to have to turn
> around is 6 to 12 months time and remove the majority of exception
> handling code because I found it was causing issues when the code was
> profiled.
If the addition of exceptions causes poor performance on a loaded system
then the hardware wasn't spec-ed properly in the first place. IMHO