in my faculty we where not taught about this GOTO thing, it was mentioned,but they told us in the begining that they are not gonna teach us (i go into an informatics faculty).
so what do you guys think, is that good or a bad thing?
I think the anti-goto stance is wildly over-hyped. Rather than saying "Use goto correctly, moderately and sparsely" people just say "Don't use goto EVER!!!" (probably because it is easier to say not to use it than it is to explain the situations in which it is good and bad).
goto is useful in some situations, such as when you need to exit a function from several places and perform some common cleanup operation. They use it quite alot in the Linux kernel.
In C++, one has to be _very_ careful about using goto -- it bypasses constructors and destructors. That can lead to very
subtle bugs that, IMHO, just aren't worth it. I have never met an algorithm where a goto was the best choice of control
structure.
The counterarguments people often make aren't very powerful. Some say: "but I have this function where I'm six levels
deep in a for loop and I need to break out". I say: "what the ^&@&!* are you doing writing a function with that many
levels of nesting?" Others say: "but I need to perform common cleanup". I say; "RAII. Use it."
(no offense chrisname -- I know you are just saying what the Linux kernel does).
I agree one hundred and fifty THOUSAND percent. I think it's important to know what it does because you never know when you'll come across code that wasn't written by you that uses it. Personally, I've never found a practical use for it, because like jsmith said, there's usually much better ways to do things (especially in C++).
One thing to consider when reading said article is that it focuses wholly on structured analysis designs -- Pascal, mostly, with some mention of C -- and fails to mention at all object oriented languages where bypassing constructors and destructors leads to outright bugs.
There are lots of things in heaven and earth that lead to 'outright bugs' that are not mentioned. I'm not quite sure I understand "it bypasses constructors and destructors" as any bad programing will do this. [edit] I don't know if I have misunderstood you, maybe an example would help. [/edit]
If it doesn't have a use it would not be there, and as it is there, learn to use it properly and don't pretend that it does not exist. [edit] This is my view on the use of goto not a comment on any other post.[/edit]
Even in this forum I have seen posts (be knowledgeable people) that replace 'goto code' with other code that was not semantically identical. When the semantically identical was posted it was, in my mind harder to follow and ugly compared to the goto code.
Use of gotos is a matter of religion. My dogma is that in modern languages, you can easily replace nine out of ten gotos with equivalent structured constructs. In these simple cases, you should replace gotos out of habit. In the hard cases, you can still exorcise the goto in nine out of ten cases.
I agree with this. For the record, I can't remember the last time I used a goto. I think that they are ok to use in certain circumstances but I always try to avoid using goto.
Personally I think that GOTO was allowed to sneak into C++ because of it's use in C and other languages. The decision to make was either rewrite X hundred number of useful C libraries ensuring a never ending stream of bugs and strange behavior; or allow GOTO so that C++ authors can use already built and ready to use C libraries.
Using GOTO is just one of those things that will never go away.
EDIT: I will say that I have found ONE use of GOTO, when paired with #ifdef or #ifndef in preprocessor directives but I have a feeling there's a better way to do this.
Yes, exactly. C++ is meant to be backwards compatible with C, and C was invented by the same folks who wrote the first version of Unix, and it had to be as fast as possible. This meant keeping the number of functions fairly minimal, since function calls incur overhead. Having said that, with long functions in C, yes, if you have cleanup code to execute, a goto may be the best solution. In the 1970s compiler optimizations would have been non-existent. One could therefore extrapolate its original existence as an optimization I suppose, and its continued existence because of backwards compatibility. Nowadays compilers are good enough that the speed difference, if any, is negligible. Perhaps in some cases there is a speed gain by using goto, perhaps in others it is faster to use control structures.
I, for one, am not in the practice of taking goto-laden code and eliminating the gotos, so I don't encounter these "translation" problems that seem to form one of the premises of McConnell's arguments. But what amuses me the most about his article is his first example.
He takes pascal code -- a repeat - if - until loop using a goto, converts it to a while loop with no ifs, claims (rightfully) it is buggy, then fixes it by rewriting it back into a repeat - if - until loop that is identical in overall program structure to the goto-version, just without the goto (yes, there is an extra check).
His second example, under error processing and gotos, is just a limitation of the language. Presumably there is some work to do to destroy a FILELIST_T, though it is not specified, and that is why the DeletePurgeFileList() function is called at the end. In C++ we have objects whose destructors handle the cleanup without the need for explicit function calls.
It's in there for backwards compatibility, so what is C#s excuse for having a goto statement?
He takes pascal code....
Steve McConnell wrote:
Most textbooks don't help since they merely provide a trivial example of rewriting code without a goto and think they're done. Here's an example of a trivial piece of code from such a textbook:
I get the impression that you have only skimmed the text but anyway.
It was a bit of a rhetorical question, it has been said that modern languages don't need the goto jump and is suggested that it is only in C++ because of backwards compatibility and yet it is still present in a modern language without and need for backward compatibility.
I posted a link to a chapter of a book that, to my way of reading, says learn how to use goto correctly and try to avoid using it unless you have a very good reason to do so and here are some rules to follow when dealing with gotos so you don't fall foul of them. Yes it talks about other languages but hell, when have you ever seen goto do anything other than what goto does?
But anyway it is not worth continuing this, the link is there, anyone can read it if they want and use this information if they find it useful.