need argument for try catch blocks

S

Stefan Ram

Fred Zwarts said:
Should the destructor ignore it and let the program continue,
or is it better to let the exception leave the destructor, so that the
program will terminate,

The common answer in the Java world is to log the inner exception,
so that it does not get lost, but can be found in some log file.

What else needs to be done is application specific, so there cannot
be a general rule for it. (That's the reason why client code is made
to handle exceptions of a library at all. Would it be obvious what
to do independent of the application, the library would already
have done it itself.)

When a programmer writes code for himself, a program terminated by
some exceptions is usable as the programmer then can trace down what
happend. A customer may not be able to interpret and handle this.

A program intended for a customer should give some
explanation of what happend in a language the customer can
understand. At least something like »An unexpected error
happened while trying to close the database link. Since this
application needs an intact database, it had to be terminated.«

For this to happen, code might catch the exception, log the
exception, emit a user-readable message, and then rethrow
the exception, which might have the consequence of
terminating the application.
 
K

Keith H Duggar

Indeed. What you say is all the MORE reason to start reshaping code so that
all resources have an assigned handler. †You have NO chance to get rid of
leaks otherwise. Only to shift them around and make subtler. † Dragging in a
swamp of code in the meantime that could be not there at all.

Except there is a huge problem: unless I'm misreading the rants
Christopher thinks that OO is the "One True Way" or some Silver
Bullet. He is annoyed that he can't blindly "follow most of the
OO principles" nor "convince" more experienced programmers that
his youthful obsession with OO is wonderbar.

He would do far better to accept that C++ is a multi-paradigm
language and to learn the proper place for tools he is not so
familiar with such as those evil *gasp* free functions (what
he called "global" functions above). And, ZOMG, macros! Such
as ON_BLOCK_EXIT which Paavo suggested.

Overall the ranting seems like a rant that would come from a
relatively young programmer who's drunk the OOP/exceptions
Kool-Aid (maybe coming from a Java background?).

That said, crappy code is crappy code regardless of what
paradigm it most resembles. Maybe the code base he is working
on truly does just suck. I'm just saying youths would be wise
to avoid being blinded by paradigm bias. Especially in C++.

KHD
 
Ö

Öö Tiib

He would do far better to accept that C++ is a multi-paradigm
language and to learn the proper place for tools he is not so
familiar with such as those evil *gasp* free functions (what
he called "global" functions above). And, ZOMG, macros! Such
as ON_BLOCK_EXIT which Paavo suggested.

Free functions are anyway perfectly OO. Free, non-friend function is
even sort-of more OO than a member function since it can manipulate
its arguments only using public interface of those and does not screw
around with their internal details.
 
C

Christopher

Except there is a huge problem: unless I'm misreading the rants
Christopher thinks that OO is the "One True Way" or some Silver
Bullet. He is annoyed that he can't blindly "follow most of the
OO principles" nor "convince" more experienced programmers that
his youthful obsession with OO is wonderbar.

Never said that. You are surely inserting words into my mouth. I am
hardly youthful either. What I do expect is that when we are working
on a project in which it was officially decided that the language
choice was to be C++ and it was officially decided that project was to
be written in an object orientated manner, that it is free and clear
of C-Style code. The only exception being where 3rd party APIs such as
the Windows API require a bit of C-style coding, but even then it
should be wrapped up in a C++ interface.
He would do far better to accept that C++ is a multi-paradigm
language and to learn the proper place for tools he is not so
familiar with such as those evil *gasp* free functions (what
he called "global" functions above). And, ZOMG, macros! Such
as ON_BLOCK_EXIT which Paavo suggested.

I am not complaining about free functions. I am complaining about free
functions that depend on a particular instance of an object and it's
member data. They have no business being outside the class. Worse are
those free functions that disregard resource allocation and release.

I will argue to the death however, that MACROS are indeed bad, so you
got me there. I will never write a MACRO myself. I can't help it if
some library makes use of one. If you'd like to start a new thread and
argue why they are good or bad, than feel free.
Overall the ranting seems like a rant that would come from a
relatively young programmer who's drunk the OOP/exceptions
Kool-Aid (maybe coming from a Java background?).

I've used Java once in my life for a three month stint. I've been
programming since I was 8 years old and I am now 34. What purpose does
your personal attack serve anyway?
 
K

Keith H Duggar

Never said that. You are surely inserting words into my mouth. I am
hardly youthful either. What I do expect is that when we are working
on a project in which it was officially decided that the language
choice was to be C++ and it was officially decided that project was to
be written in an object orientated manner, that it is free and clear

None of which you stated anywhere in this thread. If you are going
rant in vague unclear terms then you should expect some "reading
between the lines". And if someone misreads you (note "unless I'm
misreading the rants" above) most likely it was because you were
ranting instead of presenting clear points.

Indeed, most of this thread has centered on how /you/ were going
to /convince/ one or more of the team to follow this-and-such. Now
we are hearing for the first time that "it was officially decided".

Anyhow, any project manager(s) that "decided [a] project [is] to
be written in an 'objected orientated manner'" in C++ is plain
ignorant. They are throwing away maybe two-thirds of what the
language has to offer.
that it is free and clear of C-Style code. The only exception
being where 3rd party APIs such as the Windows API require a bit
of C-style coding, but even then it should be wrapped up in a C++
interface.

And there you continue to repeat the fallacy that "C-Style" code
stands in oppostion to OO. That misunderstanding lies at the heart
of your ranting in my opinion.
I am not complaining about free functions. I am complaining about free
functions that depend on a particular instance of an object and it's
member data. They have no business being outside the class. Worse are
those free functions that disregard resource allocation and release.

Here are your exact words:
There are classes where the functionality is partly handled by
global functions and partly handled by class

Where does it say "depend on a particular instance of an object
and it's member data"? And w.t.h does that even mean? How would
a function /not depend/ on the value of a object? Maybe you mean
it's accessing member variables directly? But who can know given
how vague and unclear the rant is. Again, if you are going to
rant, don't be surprised when you are misread.

Modern thinking has is that free functions can /increase/
modularity and encapsulation compared to member functions.
That seems far from your way of thinking.
I will argue to the death however, that MACROS are indeed bad,
so you got me there. I will never write a MACRO myself. I can't
help it if some library makes use of one. If you'd like to start
a new thread and argue why they are good or bad, than feel free.

Then you will never be able to achieve some sophisticated goals.
See for example Boost which has some macros. And you might also
bend over backwards and jump through hoops to avoid them even if
they happen to be the right tool for the job. That's certainly
not what I would call a professional attitude.
I've used Java once in my life for a three month stint. I've
been programming since I was 8 years old and I am now 34.

That's why it was a parenthetical remark as there are many vendors
of OO-Kool-Aid. Java just happens to be a common one. Whatever the
source, it seems clear you are drinking OO and anti-"C-Style" Kool-
Aid from somewhere.

Sure, rolling your own "virtual functions" in "C-Style" (as used
to be done) would not make sense in C++, most of the time. But you
haven't touched on any such topics. So far there have just been
vague asperions cast on free functions, pointers, macros, global
variables, etc (ie all the usual suspects for OO zealots) with no
substance and all of which are legitimate /C++/ tools.

You even began this thread with an attitude of "I'm right and
need some Kool-Aid arguments to 'convince' this [stupid] 'C-Guy'
how wrong he is." Turns out you were the one who was wrong ...
I think that signals a need for introspection, don't you?
What purpose does your personal attack serve anyway?

The goal is to hypothesize about and understand better the origin
(in this case you a person) of these rants. Since the rants are so
vague and unclear, full fallacies such as "C-Style" etc, it is
necessary to fill in the blanks and/or speculate a bit.

KHD
 
I

Ian Collins

Anyhow, any project manager(s) that "decided [a] project [is] to
be written in an 'objected orientated manner'" in C++ is plain
ignorant. They are throwing away maybe two-thirds of what the
language has to offer.

Any organisation that allows project managers anywhere near a technical
decision is doomed!
 
K

Keith H Duggar

Anyhow, any project manager(s) that "decided [a] project [is] to
be written in an 'objected orientated manner'" in C++ is plain
ignorant. They are throwing away maybe two-thirds of what the
language has to offer.

Any organisation that allows project managers anywhere near a technical
decision is doomed!

Unless you are lucky enough to work at a place where the
project managers are themselves first class engineers ;-)

KHD
 
Ö

Öö Tiib

Anyhow, any project manager(s) that "decided [a] project [is] to
be written in an 'objected orientated manner'" in C++ is plain
ignorant. They are throwing away maybe two-thirds of what the
language has to offer.

Any organisation that allows project managers anywhere near a technical
decision is doomed!

Such is even not the worst case scenario. Project managers still have
some likelihood to have a technical background. Project manager should
not have enough time to think about technical details, but that is
issue with process that has not burdened her enough with other things
to do.

Doom is however awaiting the stores where the technical details are
decided by weasels in the sales department. Surprisingly several of
the old technology companies that were decade or two ago lead by
engineers have now degraded (or onto the way of degrading) into weasel
farm. I am expecting several to burst (GM like) in next decade or
two.
 
B

Balog Pal

Unless you are lucky enough to work at a place where the
project managers are themselves first class engineers ;-)

Is some list of those places available anywhere?
 
Ö

Öö Tiib

If 60% of code is C (or C-style), I wouldn't be orthodox about RAII.
Either the developers do have ways to handle debugging and so on, maybe
just slightly home-made, or maybe some ways can be gradually introduced.

If your code does throw exceptions (or calls modules that throw
exceptions out) then it is not a C anymore and so you have to go
orthodox about exception safety and RAII. Similarly if your code runs
multiple threads then you have to go orthodox about thread safety.

Attempts to ignore usage principles of tools used will result with
disaster. If you doubt then better go work in saw-mill. Few fingers
later you will learn to follow the principles of saw usage. ;)
 
C

Christopher

None of which you stated anywhere in this thread. If you are going
rant in vague unclear terms then you should expect some "reading
between the lines". And if someone misreads you (note "unless I'm
misreading the rants" above) most likely it was because you were
ranting instead of presenting clear points.

I didn't state it, because it isn't relevant to the topic, which by
the way is C++ Exceptions and where to handle them.
I replied once to your post, which is an attempt to derail the topic
into some C vs C++ argument that has already been done to death in
other threads. My point was and still is that usage of exceptions
require OO design, OO design requires OO design, and C-style code has
no business being mixed in with OO design. If a modual is programmed C-
style, than fine, the entire module needs to be consistant. However it
is poor practice to mix C-style and OO together in the same module.
The topic, which is exception handling, is one example of how mixing
the two foobars a project.
Indeed, most of this thread has centered on how /you/ were going
to /convince/ one or more of the team to follow this-and-such. Now
we are hearing for the first time that "it was officially decided".

The point of the thread was to gather evidence to back up my argument
and to ask if my argument was a valid one. Read the first post. We've
seen that my argument was not a valid one. Good! Isn't that the point
of the newsgroup? To discuss and learn?
And there you continue to repeat the fallacy that "C-Style" code
stands in oppostion to OO. That misunderstanding lies at the heart
of your ranting in my opinion.

It isn't a fallacy. It's an experience.


Who said I don't accept that C++ lends itself to different programming
stlyes? Who says I don't know the proper place for each? Who says I am
not familiar with free functions? Who says I am not familiar with
Macros? You? Evidence? Did you review my code as well?


Here are your exact words:


Where does it say "depend on a particular instance of an object
and it's member data"? And w.t.h does that even mean? How would
a function /not depend/ on the value of a object? Maybe you mean
it's accessing member variables directly? But who can know given
how vague and unclear the rant is. Again, if you are going to
rant, don't be surprised when you are misread.

It doesn't have to. The first paragraph points out bad design. The
second points out worse design. Why is that sentence hard to follow
for you? It's in pretty simple terms. What does hell have to do with
it?

---------------------------
"There are classes where the functionality is partly handled by global
functions and partly handled by class"
Or in other worse, crappy C-style code being mixed with OO code
breaking things and making life hell:

// Crappy code made a global pointer to data that should be owned by
MyClass
MyResource * resource = NULL;

// Crappy code performs functionality that should belong to MyClass
void DoSomethingElse()
{
// Crappy code doesn't check the pointer
resource->JumpUpAndDown();

// Crappy code makes calls that throw exceptions without
documentation
resource->CauseChaosAndThrowExceptions();

// Crappy code doesn't care about resource cleanup
if( resource->BadState() )
throw MyGenericException("Something unknown has happened");
}

class MyClass
{
public:
MyClass()
{
// Crappy code allocates and releases with no concept of
ownership
resource = new Resource();
}

void DoSomething()
{
// More Crappy Code

// Crappy code still doesn't care about cleanup
if( resource->IsTheSunUp() )
{
throw MyGenericException("I like nighttime");
}
}
private:
int m_number;
}

// Crappy code always seems to think main returns void
void main()
{
// Crappy code makes memory leaks
resource = new Resource();
MyClass mine;

mine.DoSomething();

// Crappy code loves unsafe casting
BYTE * data = (BYTE *)&mine;

// Crappy code loves appending things to allocated memory in an
unsafe manner
MyStructure * dataToSend = GetSomeStructure();
memcopy(dataToSend + 48, data, sizeof(data));

// Crappy code doesn't know about stringstreams or streams in
general even when they are already used in the same function
std::cout << "Ready to send data.";
sprintf("It contains %d items", dataToSend.Count());

// Crappy code loves using its own types unecessarilly
unsigned myVar = 0;
INT32_TXTP myOtherVar = 12;

myOtherVar = myVar;

delete resource;
}

// Crappy code is probably full of other errors, but is an example of
the crappy code that occurs when C-style is mixed with OO.
// It probably has syntax errors too, but who cares? It is crappy
code.
-----------------------------------------------------

Modern thinking has is that free functions can /increase/
modularity and encapsulation compared to member functions.
That seems far from your way of thinking.

Whose modern thinking?

Then you will never be able to achieve some sophisticated goals.
See for example Boost which has some macros. And you might also
bend over backwards and jump through hoops to avoid them even if
they happen to be the right tool for the job. That's certainly
not what I would call a professional attitude.

I'll live.

That's why it was a parenthetical remark as there are many vendors
of OO-Kool-Aid. Java just happens to be a common one. Whatever the
source, it seems clear you are drinking OO and anti-"C-Style" Kool-
Aid from somewhere.

If I wanted to be a C programmer, I would have spent a decade learning
C instead of C++. I'd probably have also gotten a job doing device
drivers instead of middle-tier services. I never liked kool-aid. Much
of what I drink has come from being on this newsgroup for many many
years.

Sure, rolling your own "virtual functions" in "C-Style" (as used
to be done) would not make sense in C++, most of the time. But you
haven't touched on any such topics. So far there have just been
vague asperions cast on free functions, pointers, macros, global
variables, etc (ie all the usual suspects for OO zealots) with no
substance and all of which are legitimate /C++/ tools.

Such topics aren't the topic, so why touch them?
You even began this thread with an attitude of "I'm right and
need some Kool-Aid arguments to 'convince' this [stupid] 'C-Guy'
how wrong he is." Turns out you were the one who was wrong ...
I think that signals a need for introspection, don't you?

I belive I started this topic with "I believe I am right. How do I
convince this other guy I am right? Is there support for my argument?
Is my argument incorrect?" Pay attention to the last question there
and try roleplaying again.
The goal is to hypothesize about and understand better the origin
(in this case you a person) of these rants. Since the rants are so
vague and unclear, full fallacies such as "C-Style" etc, it is
necessary to fill in the blanks and/or speculate a bit.

You'd probably have a better chance accomplishing your goal in a
thread about C-style programming vs OO programming. Your purpose does
not match mine any more than your preferences on programming style.
 
K

Keith H Duggar

I didn't state it, because it isn't relevant to the topic, which
by the way is C++ Exceptions and where to handle them. I replied
once to your post, which is an attempt to derail the topic into
some C vs C++ argument that has already been done to death in
other threads.

Your topic was drenched from the very beginning with "C-style" vs
C++ garbage. Viz:

"I've got my self an old c style programmer ..."
"why [is] it even worthy ..."

pure unnecessary and unwarranted condescension right from the start.

It's no wonder that you "cannot formalize [your] argument" as you
are reacting emotionally to this [stupid] "old C-guy" getting "in
your way".

Not only is "C-style" so vague as to be effectively meaningless
(especially these days), but it entirely unnecessary if you have
clear and concrete examples and points to begin with.

Crappy code is crappy code. It has absolutely nothing to do with
"C style" or "C++ style". Your choice to continue associating crap
with "C-style" and good with OO is plain ignorant.

[snip more bs about meaningless "C-style"]
The point of the thread was to gather evidence to back up my argument
and to ask if my argument was a valid one. Read the first post. We've
seen that my argument was not a valid one. Good! Isn't that the point
of the newsgroup? To discuss and learn?

Of course. So what is the point of your vague "C-Style" ranting?
In other words, consider this rewrite of your op:

In my view, when your dealing with a huge project with poor
documentation, it is better to always surround anything that
throws with a try catch block, even if it seems unnecessary. I
can't count the number of hours I've spend debugging code that
was bugged because of no error checking.

The code changes all the time and a catch that might never get
hit, could get hit after many edits. If nothing else it seems
like a good way to assist debugging. It says to me, "Hey an error
could occur here!"

Some of my peers disagree and this many of the try catch blocks
I add are unnecessary. I cannot formalize my argument. Is there
an source with clout that will back my argument up? Or perhaps
someone can put into better words what I am describing? Or am
I just wrong?

There that's better, no "old C-guy", no "unworthy" reviews from
peers, no meaningless "C-Style" complaining. You derailed your own
topic before it even left the station with your bitter C-style
ranting.
It isn't a fallacy. It's an experience.

No, it's you equating the "crap" code you've run into with
"C-style" which is fallacious non-sense. In other words, it
is a fallacious inference from an experience. Crap C code
is no more "C style" than crap C++ code is "C++ style".
What does hell have to do with it?

See below. You clearly /feel/ as though you are being put through
hell by these "old C-guys" and their "crappy C-style" code.
Or in other worse, crappy C-style code being mixed with OO code
breaking things and making life hell:

More fallacious "C style == crappy" crappy thinking.
// Crappy code made a global pointer to data that should be owned by

Finally! You get it right this time. It's simply "crappy code".
Absolutely nothing whatever to do with "C-style".

[snip crappy code examples with no reference to "C-style"]

Good, you finally provided some concrete examples of crappy /C++/
code. "C-Style" is meaningless and irrelevant.
// Crappy code is probably full of other errors, but is an example of
the crappy code that occurs when C-style is mixed with OO.

No, it is an example of crappy C++ code plain and simple. It is
not C code nor "C-style" except to someone ignorant of non-crappy
C code.
// It probably has syntax errors too, but who cares?
It is crappy code.

Exactly. It is crappy /C++/ code. It is not C code nor "C-style"
code nor a mixture of that "C-style" phantom and OO.
Whose modern thinking?

Many. For someone who "[has been] on this newsgroup for many many
years" you certainly have missed some very important discussions.
Let me help you with one link from a decade ago:

http://drdobbs.com/cpp/184401197

You can proceed further from there on your own. Needless to say
it has been discussed thoroughly in the very newsgroup that you
have been reading for many many years.
I'll live.

Excellent attitude. Quiet consistent with one who considers
their peer to be an unworthy old C-guy.
If I wanted to be a C programmer, I would have spent a decade
learning C instead of C++.

Had you spent even 3 years actually learning good C programming
you would have known that this crappy code you are ranting about
is not "C-style" any more than crappy C++ code is "C++ style".
I'd probably have also gotten a job doing device drivers instead
of middle-tier services. I never liked kool-aid. Much of what I
drink has come from being on this newsgroup for many many years.

Unfortunately you missed many discussions that would have evolved
your opinions rather than reinforcing them.
You even began this thread with an attitude of "I'm right and
need some Kool-Aid arguments to 'convince' this [stupid] 'C-Guy'
how wrong he is." Turns out you were the one who was wrong ...
I think that signals a need for introspection, don't you?

I belive I started this topic with "I believe I am right. How do I
convince this other guy I am right? Is there support for my argument?
Is my argument incorrect?" Pay attention to the last question there
and try roleplaying again.

Ah yeah right. Only suckers fall for last line fine print
disclaimers beneath paragraphs of bitterness.
You'd probably have a better chance accomplishing your goal
in a thread about C-style programming vs OO programming.

If your goal is discuss exception handling and exception safe
programming in C++ you can leave aside "C-style" vague non-sense
ranting entirely.
Your purpose does not match mine any more than your
preferences on programming style.

lolz ... you are clueless.

KHD
 
Ö

Öö Tiib

I didn't state it, because it isn't relevant to the topic, which by
the way is C++ Exceptions and where to handle them.
I replied once to your post, which is an attempt to derail the topic
into some C vs C++ argument that has already been done to death in
other threads. My point was and still is that usage of exceptions
require OO design, OO design requires OO design, and C-style code has
no business being mixed in with OO design. If a modual is programmed C-
style, than fine, the entire module needs to be consistant. However it
is poor practice to mix C-style and OO together in the same module.
The topic, which is exception handling, is one example of how mixing
the two foobars a project.

Safe and elegant exception handling takes RAII. RAII is a technique of
using a non-mandatory OO feature (destructor) for automatic resource
management. Usually the RAII effect is achieved in C++ using "generic
programming" that is entirely different paradigm than "object oriented
programming". That has something to do with OO, but you are wrong that
it "requires OO design". Your usage of "C-style" as equivalent of
"lousy" is mistake, KHD wins you there easily. There even exists C-
style of OOP. Because of your confusion with programming paradigms and
other terms you should avoid pretending to be language lawyer or
architect in your team.

You have put it right is that inconsistent style is bad, but no one
will argue that. However, your original idea of putting masses of
try's everywhere wasn't good style. I think the ugliest code base that
i have seen did something like that. Good style is with lot of
throwers and with rare catchers. Despite there are lot of throws it
should actually throw very rarely. Since you fumbled like that you
should avoid trying to define the style in your team.

How you can gain (or restore?) your reputation? I have one idea: It is
lack of unit tests that often causes masses of unexpected work in
projects with large rapidly changing code-base. Do you know some
script language? Doesn't matter, these take a weekend to learn anyway.
Go write some scripts that generate unit test stubs for your code
base. Then make tests. Don't forget to test exception safety. When
there are unit tests then it is lot less likely that someone writes
something and breaks it (whatever style he uses). So team mates will
hopefully see you as useful again.
 
E

Edek

If your code does throw exceptions (or calls modules that throw
exceptions out) then it is not a C anymore and so you have to go
orthodox about exception safety and RAII. Similarly if your code runs
multiple threads then you have to go orthodox about thread safety.

Exception safety does not necessarily require RAII. It depends,
case-by-case. Sometimes, one can mix large parts of code which is
too expensive to 'convert' to RAII with wrapping code that RAIIfies it.

I agree that RAII, or close to RAII, would be a reasonable goal: what
I'm saying is that does not happen in a glimpse and you have to live
those couple of months/years plus bug fixing with non-RAII code.

Do you know a QuickRAIIFierTool download page? :) I'm half-serious,
one should write code transformation tools such as this one.
Attempts to ignore usage principles of tools used will result with
disaster. If you doubt then better go work in saw-mill. Few fingers
later you will learn to follow the principles of saw usage. ;)

I must learn to use this metaphor...
 
C

Christopher

Safe and elegant exception handling takes RAII. RAII is a technique of
using a non-mandatory OO feature (destructor) for automatic resource
management. Usually the RAII effect is achieved in C++ using "generic
programming" that is entirely different paradigm than "object oriented
programming". That has something to do with OO, but you are wrong that
it "requires OO design". Your usage of "C-style" as equivalent of
"lousy" is mistake, KHD wins you there easily. There even exists C-
style of OOP. Because of your confusion with programming paradigms and
other terms you should avoid pretending to be language lawyer or
architect in your team.

Again, I am not equating C-style to lousy. I am equating the mixing of
C-Style into OO C++, as the source of months of debugging, and
therefore lousy. 9 times out of 10 the person doing this doesn't even
have a basic understanding of the C++ language, but they are some
senior person whom has programmed C all their life. I am not confused
with programming paradigms.

From http://www.generic-programming.org/:
"The Generic Programming process focuses on finding commonality among
similar implementations of the same algorithm, then providing suitable
abstractions so that a single, generic algorithm can cover many
concrete implementations. "

From: http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
The technique was invented by Bjarne Stroustrup[1] to deal with
resource deallocation in C++. In this language, the only code that can
be guaranteed to be executed after an exception is thrown are the
destructors of objects residing on the stack. Resources therefore need
to be tied to the lifespan of suitable objects in order to gain
automatic reclamation. They are acquired during initialization, when
there is no chance of them being used before they are available, and
released with the destruction of the same objects, which is guaranteed
to take place even in case of errors.

In order to even have constructors and deconstructors we are implying
we are making objects in the first place! Please tell me how you can
have RAII without OOP. Generic Programming doesn't explictly imply
objects at all. Regardless, C-style programming implies neither of
these.

You are both also talking as if I am introducing the exceptions. Nay!
I am trying to handle them! Someone else was responsible for throwing
exceptions left and right from c-style code that had a disregard for
freeing of resources.
 
C

Christopher

How you can gain (or restore?) your reputation? I have one idea: It is
lack of unit tests that often causes masses of unexpected work in
projects with large rapidly changing code-base. Do you know some
script language? Doesn't matter, these take a weekend to learn anyway.
Go write some scripts that generate unit test stubs for your code
base. Then make tests. Don't forget to test exception safety. When
there are unit tests then it is lot less likely that someone writes
something and breaks it (whatever style he uses). So team mates will
hopefully see you as useful again.- Hide quoted text -

I didn't realize my entire team was reading this thread and my
reputation could be ruined by one simple question! I don't have a
reputation to ruin on my team. My team is neither involved in this
discussion or its outcome. While they may by happenstance wander onto
this newsgroup, chances are slim. If they do, I don't mind. I haven't
written any code here yet, so there is nothing for me to design or
test. I am fixing the mess that existed before my joining. At the day
of my hire, I was handed the source code for a windows service that
will not even start up 9 times out of 10, because of one unhandled
exception or another. 3 months of handling them one by one and they
continue to appear. When it does start up, it runs for all of an hour
or two before crashing. I am hardly worried about my reputation at
this point. I am worried about fixing the source of the problems and I
think that source is in someone's thinking rather than in the code.

At any rate, the question has already been answered. Boundries need to
be identified and catching and handling needs to occur there. I am
happy with that.
 
Ö

Öö Tiib

Exception safety does not necessarily require RAII. It depends,
case-by-case. Sometimes, one can mix large parts of code which is
too expensive to 'convert' to RAII with wrapping code that RAIIfies it.

If exceptions are thrown by code that is called by such "too
expensive" then it is thrower who has to be converted into non-thrower
by wrapper. I don't get how it can be other way around.
I agree that RAII, or close to RAII, would be a reasonable goal: what
I'm saying is that does not happen in a glimpse and you have to live
those couple of months/years plus bug fixing with non-RAII code.

Do you know a QuickRAIIFierTool download page? :) I'm half-serious,
one should write code transformation tools such as this one.

No i don't know. Overall there is rather sad set of C++ refactoring
and round-trip engineering tools available. Sad even if to include
commercial ones. The ones that are available are often expensive but
still must be applied carefully to not damage more than cure.

Also be careful when considering writing your own tool. Usually every
team contains a dedicated tool-dwarf or couple, but tools that
outright refactor source code are made rarely.

Typical problems are that the C++ itself is most complex programming
language in use, source material is always defective, an human is
involved in configuring and assisting the tool but may make mistakes
and tool itself is also likely defective (or with "softer"
terminology: inflexible, too mechanical and not enough self-
critical).

The C++ code base should be therefore covered with unit tests rather
well first for any sort of refactoring to be applied to it at low pain
level.
 
E

Edek

No i don't know. Overall there is rather sad set of C++ refactoring
and round-trip engineering tools available. Sad even if to include
commercial ones. The ones that are available are often expensive but
still must be applied carefully to not damage more than cure.

Also be careful when considering writing your own tool. Usually every
team contains a dedicated tool-dwarf or couple, but tools that
outright refactor source code are made rarely.

Just to clear the possibility: the team I'm with doesn't need one.

I'm rather considering this as a 'make money' option in some distant
future (read: probably never)
Typical problems are that the C++ itself is most complex programming
language in use, source material is always defective, an human is
involved in configuring and assisting the tool but may make mistakes
and tool itself is also likely defective (or with "softer"
terminology: inflexible, too mechanical and not enough self-
critical).

Yeah. I always prefer even to indent the code myself than rely on an
automata, maybe the IDE is helping more than it is annoying. And most
source analysis tools for me are more of a pastime that an everyday
tool.

The first thing I thought of when thinking about how to write such tools
is that C++ is indeed complex to parse, let alone transform.
I don't agree with the defective argument: most simple refactoring
mechanisms built into IDEs fall back to 'Preview' allowing human
intervention, either when the tool is not sure or just for the sake
of checking.
The C++ code base should be therefore covered with unit tests rather
well first for any sort of refactoring to be applied to it at low pain
level.

Actually, I always consider refactoring more an obstruction to doing
unit tests (or the other way round...);
I do agree that having unit tests vastly helps when doing ANY
changes.
 
S

Stefan Ram

Christopher said:
In order to even have constructors and deconstructors we are implying
we are making objects in the first place! Please tell me how you can
have RAII without OOP.

On can get rid of most of the features of OOP (such as
subtyping, polymorphism, abstraction [data hiding]).

It would be sufficient to add a few extensions to C:

{ __resource struct example *const e =
malloc( sizeof( struct example ));
... }

The »__resource« keyword requests a primitive kind of
RAII, which is implemented as follows:

»struct example« (or whatever other struct used with
this) must be have been defined as:

struct resource
{ int( *cleanup )( struct resource *resource ); ... }

, i.e., it must start with a pointer to a function taking
a pointer to such a struct as an argument.

The C+RAII compiler will then make sure that this function
is called upon block exit with the object pointer as an
argument, whenever the object pointer is not 0.
 
Ö

Öö Tiib

In order to even have constructors and deconstructors we are implying
we are making objects in the first place! Please tell me how you can
have RAII without OOP. Generic Programming doesn't explictly imply
objects at all. Regardless, C-style programming implies neither of
these.

C++ language contains two features for generic programming:
preprocessor and templates. First of those is present in C too. All
popular frameworks use both features. Paavo Helde already replied
that "how" with link to ScopeGuard (that again uses both tools) to
introduce RAII into C-style code at low amount of refactoring made
with ON_BLOCK_EXIT().

There are indeed destructors used internally in ScopeGuard, but you
don't have to write any of them. The applied generic programming does
it for you. Better way is indeed to rewrite all the C-style code but
it is up to you to consider what makes more sense in your situation.
On any case arguing with us does not help. We have our situations and
you have yours.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top