How about
Code:
if ( open_database() ) {
if ( read_database() ) {
// do stuff
} else {
// error reading database
}
// close database
} else {
// error opening database
}
More often than not, the sudden urge to use goto is a sign of poor design - for example digging your way out of a large function which should have been split into several smaller functions long ago.
If your functions are small and have a clearly defined purpose, then there shouldn't be a need for goto.
A massive rambling function trying to do too much all but makes goto a necessity, but the real fix is to redesign it.
> it's clearer and easier to maintain than repeating a "database close"
Well I usually think in terms of matched pairs of functions, something along the lines of
Code:
FILE *fp = fopen( "file", "r" );
if ( fp != NULL ) {
do_stuff( fp );
fclose( fp );
} else {
// open error
}
The success (or otherwise) of do_stuff() doesn't matter to this bit of code, the file is opened in exactly one place, and closed in exactly one place.