[quote=“opiop65,post:3,topic:45476”]
Actually, not really. The evil reputation goto has comes from the old days when you could use it to jump to any point at all in the program. That tremendously increases the cost of maintenance, because any time you edit a section of code you need to be extra careful about updating or removing any gotos if you need to. This was especially true in languages like BASIC and FORTRAN, where you used line numbers to branch around:
10 PRINT "Hello World"
20 GOTO 10
In C and C++, this is no longer an issue. The goto statement (it’s not a function) is restricted to the current function scope. You can’t jump to arbitrary locations in the program. And, you have to use named labels, so modifying code doesn’t modify the point you jump to as it does when line numbers are the target. A common idiom in C, where there are no exceptions, is to use goto for error handling in cases where it makes sense. For example, imagine this totally contrived hypothetical function:
foo* create_foo( int x ) {
foo* foo = create_foo();
if( !foo ) goto FAILURE;
if( !do_something_with_foo( foo ))
goto FAILURE;
if( !do_something_else_with_foo( foo ))
goto FAILURE;
return foo;
FAILURE:
log_error( "Failed to create foo." );
if( foo ) deallocate_foo( foo );
return 0;
}
I’ve done a lot of C over the years and have used this idiom often, particularly in resource loaders. It’s a very useful, and very safe, idiom. The evil reputation, which was rooted in real issues, does not apply in modern languages, but still lingers. Much like the Java performance myth.
That said, using goto for loops certainly is considered abuse!