Re: Why do some code bases don't use exceptions?
On 25 Nov., 12:14, "dragan" <spambus...@prodigy.net> wrote:
peter koch wrote:
On 24 Nov., 10:48, James Kanze <james.ka...@gmail.com> wrote:
When not using exceptions, you more or less write the hidden error-
returning path explicitly in your code. In my experience, this often
gives a signicifand increase in source-code size
Can you give some numbers?
Not any exact ones, but I have been working in places where exceptions
were not used (due to some code being quite old and no one caring
about fixing). Code there typically looked something like:
int func(parm p,std::string &result)
{
std::string s;
int error;
result = func_1(parm,s);
if (error != OK)
{
return error;
}
error = func_2(s);
if (error != OK)
{
return error;
}
result = s;
return OK;
}
instead of the much simpler
std::string func(parm)
{
return func_2(func_1(parm));
}
This example is a bit exaggerated, as the function body does not do
very much, but it not atypical. One side effect of this style was that
functions typically returned a status, making calling the functions
more complicated. My guess is that the code length was at least
doubled this way. The above function has a 10-fold increase in lines
(disregarding braces), and it is not that easy to figure out what is
going on.
and also obscures the
algorithm.
I don't see why. Hide the details with macros.
First that would require you to use macroes, which can lead to more
errors. Secondly: How would you hide the details in the code above?
This is a source of bugs,
Yes, probably, without any formalism such as macros. I guess you could go a
step further and write a preprocessor do do some instrumentation for you
also.
Yes. You could also use another language. If you get so far out that
you need to write a preprocessor that proposition might acutally be
worth following.
and the effort required to
explicite the path is normally much larger than the effort used to
write exception safe code.
I think that would be the same or maybe harder with exceptions because of
the interactions with other C++ stuff (not that I can name that other stuff
though). Ensuring cleanup and RAII and call order and the like is an
on-going thing no matter what your EH strategies are.
As you, I see no interaction with other C++ stuff. Exceptions fit
perfectly with C++.
/Peter