-
Quote:
Originally posted by John E
I think the RAII technique you described is similar to the principle of smart pointers, which I use quite extensively - but I'd never heard the term RAII.
Yes, you are right...a smart pointer is following exactly this technique... :cool:
Quote:
Originally posted by John E
For my money, this is the only weakness of try/catch logic. The option:-
catch(const CError &refcException) // or whatever
can only be relied upon in code that you wrote yourself. Too many times I've come across documentation stating that function X throws an 'int' - which it probably did at one time - except that the code got updated but the documentation didn't.
That is of course one of the biggest problems. However, that is not only a problem while using exception handling. But that is exactly one of the drawbacks, however, usually you can enclose you entry routine with a general 'catch(...)' to prevent crashing and allow a successful closage of the application.
The above description is even worse in regard to exception specifications. They are supposed to clearly indicate which type of exceptions will be thrown by a function. Nevertheless, this serves only documentary purposes since your above described problem counts here as well... :cool:
-
Quote:
Originally posted by John E
Oh - one other question.....
Where the thrown exception object is a pointer - e.g.
Code:
catch(some_exception_object * pException)
is it always safe to assume that the object being pointed to will need to be deleted? - e.g.
Code:
catch(some_exception_object * pException)
{
. . . // do whatever needs to be done
delete (pException); // :confused: - Would this always be the case?
}
Yes...while throwing pointers around you usually have to delete them, nevertheless this kind of depends what the type of exception handling is supporting... :cool:
-
Actually it depends on how you throw the exception. If you throw it with
throw new CException("Error occurred!");
then you need to delete it.
If you throw it with
// global
CException globalException;
throw &globalException;
then you cannot and must not delete it.
In general, it is not a good idea to throw pointers. Stick with throwing temp objects and catching references, exception classes should be small anyway.
Oh, and in the MFC it's safe to assume you gotta delete the exception pointer.
-
Quote:
Originally posted by CornedBee
Oh, and in the MFC it's safe to assume you gotta delete the exception pointer.
Well...and failing in doing so in many examples... :cool:
-
I don't use a goto since the old times of BASIC.
Haven't you ever read the famous article "GOTO statement considered harmful" by W. Dijkstra? (may 1968)
-
I think Yves posted a link to it in another thread but although I followed the link, I couldn't find the goto comments ! :(
What did he say (in a nutshell!) ?
-
Quote:
Originally posted by Doctor Luz
Haven't you ever read the famous article "GOTO statement considered harmful" by W. Dijkstra? (may 1968)
Oh that sounds so familiar! I don't know if I read it but I know I have heard of that article. It was a part of the beginning of Structured Programming.
Go To Statement Considered Harmful
GoTo considered Harmfull
considered harmful
Perhaps someone can find the "‘Goto considered harmful’ considered harmful" article.
-
I like the conclusion in that last link - that it took several decades before everybody realsed that in fact, both sides were wrong...!
:) :)
-
Quote:
Originally posted by John E
I like the conclusion in that last link - that it took several decades before everybody realsed that in fact, both sides were wrong...!
Well..sometimes it takes some time... :D
-
Quote:
Dijkstra's article
In [2] Guiseppe Jacopini seems to have proved the (logical) superfluousness of the go to statement. The exercise to translate an arbitrary flow diagram more or less mechanically into a jumpless one, however, is not to be recommended. Then the resulting flow diagram cannot be expected to be more transparent than the original one. [1]
This is good argument for the pro-goto people ;)
-
I have used goto occaisionally mainly to jump out of deepish nested loops as it avoids extra comparisons and can save a flag variable or two.That is the only use I have found for goto. I also believe that if goto is used properly it is also safe with regards to exceptions. If you jump out of a scope the destructors that need calling get called. I'm fine with that once in a while.
-
Hi, it's me again, the great goto-fan man...
It's very interesting to see that people that have never used GOTOs, also have most arguments against using them. It my sound logical at first sight, but I'm wondering where did they get all these con-arguments from, when they've never used GOTO?!?!Have all of you read Dijkstra's famous paper Go To Statement Considered Harmful?
I doubt all of you have picked your goto dislike from his paper. It would be interesting to hear where all the others have gotten it from. Was it in school, on a programming course or by a colleague maybe?
I can very well understand people that choose to not use goto (at least in C++ where you have exceptions), but what I don't understand is this religious anti goto talk... especially from those of you that don't understand its usefulness and probably never will.
/Jonas
-
For about 20 years I was a COBOL maintenance programmer. I have seen a great variety of code. I have seen some really ugly spaghetti code. Usually when gotos are used, the program is a mess. I dislike gotos because I know they are usually associated with messy code. It is the frustration of dealing with such stuff that I get an emotional reaction to the idea of gotos.
Management that does not know what they are doing and think they are saving money can allow programs to grow into a huge mess. One credit card company had a main update program that was so huge that it was continually modified by multiple programmers at a time. Another credit card company was forced to modify all their credit card software to process multiple companies when the software was originally written to support credit cards for just that bank had to be modified when the law forced them to. Situations like that cause software to be a mess and gotos can make the work much worse.
So I think I am justified in feeling the way I do. I think people need to anticipate that there are some programmers that have had experiences with gotos that will make it impossible to get them to listen to anything you say in favor of gotos, no matter how reasonable you might be.
When Strutured Programming was new, I attended a presentation of it. In the handouts was a sample program written with gotos and another version of it written as a strutured program. The first version was difficult to understand; the structured program was easy to understand. I am sure that programs you, Jonas, wrote would be as easy to understand as possible but use of a goto just opens a door I don't want open.
-
I have learned to program roughly 6 years ago, and I have been taught that goto was a devil. For that reason, I learned to not use it, and everytime I faced a problem, I found a solution without resorting to goto. Of course, I learned C++ right away, so I always had the tools to avoid goto...
My 2 cents
-
Well, my goto experience is based on BASIC, assembler and VB. I extended some existing x86 assembler program (some 10k lines) at a company I worked as a summer job. Part of it was pretty well written, but other parts were just 'random jumping around'. It took much more time trying to understand what the program flow was than to write the extension. But well, assembler does have an excuse.
Currently, in addition to programming in C++, I also maintain a pretty big VB codebase (also some VBA, but that's another matter). I'm not saying that the programmer who originally wrote it is bad, but in some places the code really is a mess, compounded by extensive use of gotos (forward and backward). It's hard to figure out what kind of program flow to expect and what kind of run-time characteristics the thing has. It's not only the fault of the gotos, but they do contribute quite a bit to the fact that it's unreadable.
Quote:
It's very interesting to see that people that have never used GOTOs, also have most arguments against using them.
That doesn't mean that the people who don't use it have never seen or had to maintain code that does use it.
-
Quote:
Originally posted by Yves M
That doesn't mean that the people who don't use it have never seen or had to maintain code that does use it.
ditto, please serve me up a big plate of steaming spaghetti...one of the reasons I lean towards semi violently against ;) but then don't blame the game, blame the player.
-
Quote:
Original posted by improving
I have used goto occaisionally mainly to jump out of deepish nested loops as it avoids extra comparisons and can save a flag variable or two.That is the only use I have found for goto. I also believe that if goto is used properly it is also safe with regards to exceptions. If you jump out of a scope the destructors that need calling get called. I'm fine with that once in a while.
If you have to use gotos, this is one of the case that justify it´s use, you can change (on this case) the pure goto to use setjmp and longjmp, they made a "structured goto". More readable safe and legible. I don´t know exately if these instructions are on stardard but they are a elegant substitute for gotos.
Rabelo
-
Actually they are far more dangerous and applicable only where you need "rescue moves", such as escaping from recursive descent parsers.
-
Could you please explain me why are they more dangerous????
Rabelo
-
Unlike goto, they don't honor any language rules. This is especially bad for constructors and destructors, which don't get called.
-
I thought we are talking about C, C++ of course there is no necessity of goto at all, since try and catch do this job very well.
Setjmp and longjmp do honor the language rules, they form a structured block:
Code:
main()
{
if setjmp("blablabla")
{
{
{
{
{
......
{
....
....
if (unrecouvereble error occurs here....)
{
longjmp("blablabla")
}
}
}
}
}
}
}
else
{
wherever lonjjmp is called
the control falls in this point....
clean the house and exit;
}
}
}
the plus of this struct is that there is no label, so it is much more manangable.
-
This is probably an obvious question (but I've never actually used longjmp). Assuming the longjmp line was veentually reached, where would it jump to?
-
It jumps to the inside of the setjmp function. The difference is this: on return of a normal call to setjmp the return value is 1, on return when longjmp jumped into it the return value is 0.
But they don't necessarily form a structured block:
Code:
void somefunc()
{
setjmp("bla");
}
void otherfunc()
{
longjmp("bla");
}
The two can be totally unrelated. Of course, this would be bad design, but it is possible. Worse errors are possible than with any other part of C except perhaps pointers. You can jump anywhere, from anywhere. You could even reuse jump buffers!
-
This design for setjump is like you said, very very bad. You may think in terms of scope instead of functions.
Quote:
originally posted by John E
This is probably an obvious question (but I've never actually used longjmp). Assuming the longjmp line was veentually reached, where would it jump to?
Wherever lonjmp is called inside the setjump scope the block of the else is executed.
Rabelo]
-
Well it's a bit rare.
I used one a few years ago when doing some work on mixed C/inline-assembler operating system design in order to create a label which was visible within both languages.
For normal C/C++ programming it is almost always the case that something went wrong with the fundamental software architecture if a a developer is strongly motivated to use a goto statement. And this sometimes mandates the usage of the programming idiom.
Whatever...
Sincerely, Chris.
:cool:
-
Re: The grand 'goto' poll
* disintering the topic out of the place where it should lie
Nobody mentioned the very serious problems that can occur when using goto's (and corresponding non-problems that can lead to need in goto's).
As many times said in this topic, goto makes you difficult to understand the control/data flow. And how difficult does it make some automatic tool to understand that? For example, optimizing compiler, verification tool (that can find a plenty of serious logical/security errors in 2000000 lines of code in several minutes if that code was written properly)? Testing tools that can produce testing procedures based on specification and the same code to catch some errors that no rational time of experiencing or verification-by-hand of software can detect?
End of all: it even would be difficult to obfuscate that programm with some obsufcation utility bacause it requires changing of control and data flow and so requires fine understanding of it!
Of course you can write the programm with goto's right way so there would be no irreducible control graph, but using goto you can lead it to that awkward state I mentioned above...
Next: what is the cause of using goto's mentioned?
Breaking from deep loops... Well, if you use extra flag variable to do so and do not use "bad" goto's, optimizing compiler will build your programm in such a way that there would be actually no flag and you will lose no efficacy at all.
Handling exceptions: as it was suggested you can always use do{...}while(0) or return value technique.
Any other resons?
For try..catch mechanism. It is also a nightmare for compiler. And for programmer too. What if you created that exception passing by throw using RAII? It would be destroyed before handled. What about saving some data on exit (on destruction)? It would be saved in disintegrated state while lifting up the stack in hope of finding "catch". And that dependance of well documentation as well...
-
Re: The grand 'goto' poll
I really think that the serious problems were mentioned. I think they were especially mentioned in indirect ways.
Did you read the articles about gotos? They are old but they are relevant and important classics. I forget the titles; something about gotos are considered harmful and I know there are links to the articles in previous messages in this thread. They say the things you are saying and clearly enough that many of us did not consider it necessary to repeat what they say.
Are you familiar with Structured Programming as a specific methodology? It is the methodology, for example, that is part of the definition of the design of the Pascal language. One reason the methodolgy was developed was to overcome the serious problems of gotos. So the previous comments about Structured Programming are relevant to what you are talking about.
There are books about Structured Programming; I don't know if they describe the problems that Structured Programming solves, but they probably do in some very relevant ways.
I recall that I said something about programmers having a strong emotional negative reaction to gotos. I think that is also relevant, although it is not as clear as it could be.
-
Re: The grand 'goto' poll
Quote:
What if you created that exception passing by throw using RAII?
That would be pretty stupid...
-
Re: The grand 'goto' poll
I do not like and use 'goto' because I had to learn Assembler for one year now, and all these jmp, jne, je, jz, jnz, jc, jnc :sick: ... So I try to write plain and easy understandable code
-
Re: The grand 'goto' poll
Hi!
I've used goto a lot in C, to escape sub-"main menus" in my applications ("hi, CONIO fan" and things like that). Thus one, or maybe two, goto's per program. Always together and easily visible anyway. Always in main. Always nowhere else to go. I got used to it because I also used 'em in BASIC to escape menus, but back then I placed a goto everywhere I could (mostly because if its silly error checking methods).
Since i'm working in C++, i've left goto's in the memento box, but I think that, given the case, that would have to be a very TERRIFIC program to be unsolved with what C++ offers, I would use it again.
Still, I think goto's are not bad, just... incompetent (ala "humans 're not evil, just... uninformed") for what it should had to be used. I haven't ever reccommended using goto's to my former students (i'm assisting student in the computer lab).
I like this chit-chat about the goto thing, bringing me some memories of the past. Hope it does not evolve into one of these Holy Wars. Or... did it?
-
Re: The grand 'goto' poll
I use it only, ONLY, for debugging purposes. I have never used it in a final program and never will and believe that generally it should never be used.
-
Re: The grand 'goto' poll
I think I used goto once when I was just learning C back in the 1980s. I then had to maintain a program written by someone who *loved* goto. I spent months rewriting his code to make it more readable (and eliminating his blown pointers and memory leaks).
-
Re: The grand 'goto' poll
Quote:
Originally Posted by wdolson
I think I used goto once when I was just learning C back in the 1980s. I then had to maintain a program written by someone who *loved* goto. I spent months rewriting his code to make it more readable (and eliminating his blown pointers and memory leaks).
Ouch... you have my sympathies.
-
Re: The grand 'goto' poll
Quote:
YourSurrogateGod:
Ouch... you have my sympathies.
The upside was that managing that code made me a much better programmer. I learned a lot of things *not* to do and why!
-
Re: The grand 'goto' poll
Quote:
Originally Posted by wdolson
The upside was that managing that code made me a much better programmer. I learned a lot of things *not* to do and why!
There's a flip-side to everything in life :) . Like me realizing that it's a good thing that I have limited funds, because I would spend it all on Guinness :) .
-
Re: The grand 'goto' poll
I wonder how long people will still continue discussing this old subject. What amuses me most is how rigid and dogmatic most people get when it comes to gotos... After all, Dijkstra's famous paper was written way back in 1968, and we have already been taught at school why the structured programming paradigm tried to avoid gotos.
As a sidenote: During a compiler design class at university, we had to modify and extend the source code of a compiler for a language called "MiniPascal". The compiler itself was written in Pascal, and the author was nobody else than Prof. Nikolaus Wirth, the creator of Pascal and Modula 2 and one of the strongest advocates of structured programming. And guess what - his code was full of gotos (mainly for error handling). At that time, I found that fact both funny and revealing. Later, I understood that the most important point about the entire goto-discussion was to avoid beeing too dogmatic. Or, with other words: I guess that I didn't use a single goto statement in my own code since the days of BASIC (that's almost 20 years ago). Not that I tried hard to avoid them - they just weren't necessary. However, if I would run into the need to use a goto statement tomorrow, I wouldn't hesitate doing so.
For the interested, there's another very amusing (and long) thread on that subject, also known as The Famous Goto Thread. If you find the time to read it, pay special attention to galathaea's sudden illumination... :D
-
Re: The grand 'goto' poll
For the love of <insert mythical creature here> please let this thread die...
-
Re: The grand 'goto' poll
Quote:
Originally Posted by Mick
For the love of <insert mythical creature here> please let this thread die...
How about publishing a paper with the subject "Goto Discussion Threads Considered Harmful"? :D
-
Re: The grand 'goto' poll
Quote:
Originally Posted by gstercken
How about publishing a paper with the subject "Goto Discussion Threads Considered Harmful"? :D
I would...but the hippies stole all my paper...
-
Re: The grand 'goto' poll
Quote:
Originally Posted by Mick
For the love of <insert mythical creature here> please let this thread die...
Just close the darn thing...
-
Re: The grand 'goto' poll
Hey...! I was pleasantly surprised when it came back :p
More discussion neede !! :D :D ;)
-
Re: The grand 'goto' poll
When I was student, my C language teacher told us, no matter how good project we made, if we used goto even once, he wouldnt accept our project. In addition, he didn’t teach us the functionality of goto to make us goto-free programmer. I can safely say that I never use goto in my any C or C++ program and I never needed to do it.
The only use of goto, which I can recall, is in MS-COBOL and GWBasic languages, but at that time I didn’t know about structured or even object oriented programming. Oh yes, still using it in assembly language programming.
-
Re: The grand 'goto' poll
Quote:
Originally Posted by Zeeshan
When I was student, my C language teacher told us, no matter how good project we made, if we used goto even once, he wouldnt accept our project.
That's another typical example of the hysterical anti-goto dogmatism mentioned above... ;)
-
Re: The grand 'goto' poll
Wondering why this type of debate is not seen regarding the elusive "ComeFrom" instruction......
-
Re: The grand 'goto' poll
Quote:
Originally Posted by TheCPUWizard
Wondering why this type of debate is not seen regarding the elusive "ComeFrom" instruction......
Ahh, a fellow initiate of the most underappreciated language ever created:
Intercal
"Abandon All Sanity, Ye Who Enter Here"
Jeff
-
Re: The grand 'goto' poll
Quote:
Originally Posted by Zeeshan
When I was student, my C language teacher told us, no matter how good project we made, if we used goto even once, he wouldnt accept our project.
If I were a teacher/professor I would say exactly the same (actually I say it to anyone asking about goto). I would fail anyone that used goto... :cool: Call me hysterical anti-goto dogmatic but that's how I feel about it. Goto is the first thing I would get rid of from C/C++.
-
Re: The grand 'goto' poll
Quote:
Originally Posted by cilu
If I were a teacher/professor I would say exactly the same (actually I say it to anyone asking about goto). I would fail anyone that used goto... :cool: Call me hysterical anti-goto dogmatic but that's how I feel about it. Goto is the first thing I would get rid of from C/C++.
Actually, I was trying to lift that discussion to another level: My remark (and the previous, longer post) were not directly related to the use of the goto statement itself, but about a category of persons who are stubborn and inflexible enough to make a religion out of something as arcane as the general use of a single language feature. Did you want to say with your post that you belonged to that category? :D ;)
-
Re: The grand 'goto' poll
Quote:
Originally Posted by gstercken
Actually, I was trying to lift that discussion to another level: My remark (and the previous, longer post) were not directly related to the use of the goto statement itself, but about a category of persons who are stubborn and inflexible enough to make a religion out of something as arcane as the general use of a single language feature. Did you want to say with your post that you belonged to that category? :D ;)
When it comes to goto, definitely... :D ;)
Do you know that song:
Quote:
imagine there's no country
nothing to kill or dying for
and no goto
imagine all the people
living life in peace
you may say i'm a dreamer
but i'm not the only one
i hope some day you'll join us
and the world will be at one...
-
Re: The grand 'goto' poll
As you know, Liverpool is where The Beatles came from.... and a few years ago, Liverpool airport renamed itself to "John Lennon Airport" and adopted the slogan "above us only sky" - a direct quote from his song, Imagine.
Then somebody started spreading rumours around that their baggage handlers had decided to adopt another line from the same song....
"imagine no possessions" :D :D
-
Re: The grand 'goto' poll
Quote:
Originally Posted by John E
As you know, Liverpool is where The Beatles came from.... and a few years ago, Liverpool airport renamed itself to "John Lennon Airport" and adopted the slogan "above us only sky" - a direct quote from his song, Imagine.
Then somebody started spreading rumours around that their baggage handlers had decided to adopt another line from the same song....
"imagine no possessions" :D :D
Probably I would say "don't goto John Lennon Airport"... :D :wave: