Looks like this was taken from Java. Although I thought these two languages don't have anything in common except for four letters in the name.Quote:
Originally Posted by wildfrog
Printable View
Looks like this was taken from Java. Although I thought these two languages don't have anything in common except for four letters in the name.Quote:
Originally Posted by wildfrog
Those two (or more) languages are equal in many ways. My example was with Javascript in mind, but looking at the spec I now see that Java supports labeled break/continues as well.Quote:
Originally Posted by treuss
- petter
Asking this question in a C++ forum dominated by OOP guys is like throwing a bone to a dog. :D IMHO it is bad if you are doing in an OOP project. But C++ is not all about OOP so I wouldn't say that goto is essentially bad.Quote:
Originally Posted by phosphorus
This has been debated here and many other places before. In every thread I've ever seen, the conclusion has been that goto makes code less maintainable. From this perspective, goto is bad to use.
However, if you need highly optimized code where every CPU cycle counts and profiling has proved that the use of goto will get rid of a bottleneck, then the use of goto is OK. But this is pretty darn rare!
Well the absolutely worst thing you can do with a goto is if the target label appears before the goto call, i.e. the logic goes back up instead of down.
If you see code like that, then the person who coded it should be fired on the spot.
Regards,
Paul McKenzie
Actually, I have worked on projects where this was necessary! :eek:Quote:
Originally Posted by Paul McKenzie
But this was an embedded C (not C++) application running on a DSP chip with a limited amount of program memory and only done so after all other more maintainable attempts proved too costly (in program memory space) to implement.
I have seen goto used in the source code of the gcc compiler ( 10 years ago). The code was generated by lex and yacc and thus was quite safe to use as long as you didn't have to change it.
Exactly, gotos are difficult to maintain!Quote:
Originally Posted by PadexArt
My last word on the subject: goto's are bad and should not be practised anywhere, anytime and under no circumstances. For all you junior programmers out there, take note.
John
This is probably the WORST argument against goto statement I've seen (and I did see a lot).Quote:
Originally Posted by Vaderman
Your code is far more difficult to understand / maintain. It violates much more important rule: the loop variables should NOT be reassigned within the loop.
This is the most artificial way to avoid an otherwise harmless, clearly stated attempt to break from nested loops, simply for religious reasons.
I would also dare to say that almost nothing should be avoided "at all costs" - the cost just might be much greater than a potential (often misestimated) loss.
<EDIT> P.S. Even better, to my surprize, I found that if you have an object created at "// do something" line, its destructor is called when goto is executed!
i recently wrote some code like that i was tempted to use goto but bea
cause of the hysteria related to it i didn't. I always do the following:
RichCode:
void f()
{
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
// do something
if (j==10)
{
break; //we got the result get the hell out
}
if(...)
break; //add a conditional here usually identical to the if need to exit the other loop
//go go go go
}
}
// do something here
}
here is a very readable way to breakout of nested loops.
goto is just a tool. Like every tool it has the potential to be used correctly or incorrectly.Code:for(size_t i = 1, break_loops = 0; i < 10 && !break_loops; i++)
{
for(size_t j = 0; j < 10 && !break_loops; j++)
{
if(WHAT_EVER)
break_loops = 1;
}
}
The only possible arguement here is that it is too easy to use goto incorrectly.
//In the interest of flame:
if you use goto then you are guilty of bad programming style.
Just as, if you use MFC, say CArray instead of vector, you are guilty of bad programming style by proxy
There are no exceptions. That is the way it is...
Back to the original question, it is easy to answer:
NO
Using goto in C++ must not be a "practice", if practice refers to a habit, or a way of usually doing things.
I read the definitions of practice.
practice:
knowledge of how something is usually done; "it is not the local practice to wear shorts to dinner"
Furthremore, phosphorus said that he programmed with a TI 83+ calculator, where goto is a much used control structure.
It is obvious that if we answer that "it is okay to use goto", he will be tempted to write a C++ program using the same control-structure design that he was using with his calculator.
It is a an abuse of goto.
Actually, someone who learns C++ should ignore the existence of goto (especially if he used to write programs with a goto-full language).
Then, when he is an experienced C++ programmer, he can, sometimes, use goto when alternatives are worse (as the alternative Vaderman showed).
He should, however, keep in mind that a program containing more than very few gotos, is flawly designed.
PS: for the break-from-two-nested-loops thing:
When writing a C++ program, a return statement is usually enough to double-break, because a good programmer writes small routines which do a specific task, and it is seldom to have to do more work after a double-for-loop, except, perhaps, resource deallocations, but if the program has to be exception-safe, RAII is used, and no explicit resource deallocations occur.
very good point although it somewhat avoids the issue.Quote:
Originally Posted by SuperKoko
I second that.Quote:
Originally Posted by SuperKoko