Re: Goto considered dramafull
Jose Da Silva <[email protected]>
| Newsgroups | gmane.comp.fonts.fontforge.devel |
|---|---|
| Message-ID | <[email protected]> |
On August 30, 2014 06:46:17 AM Frank Trampe wrote:
> I've never liked gotos because they break the nesting model and make
> tiered clean-up difficult.
You need to pay attention to nesting when you use goto.
This is a very good example with nesting, look at this function:
fontforgeexe/lookupui.c:SFDTrimUndoOldToNew
error3SFDTrimUndoOldToNew: fclose(of);
error2SFDTrimUndoOldToNew: fclose(nf);
error1SFDTrimUndoOldToNew: fclose(retf);
error0SFDTrimUndoOldToNew:
return 0;
}
This one is less complicated:
gutils/gimagereadtiff.c:GImage *GImageReadTiff(char *filename) {
errorGImageReadTiff:
fprintf(stderr,"Bad input file \"%s\"\n",filename );
errorGImageReadTiffMem:
free(raster); free(ret);
TIFFClose(tif);
return( NULL );
}
> There are plenty of ways to avoid using them. One is to have each block
> check for success or non-failure of the preceding block before doing its
> thing. This also allows for more localized clean-up of block-specific
> data.
Agreed, other ways can be done, such as case statements or even nested
while (0) loops, or something else:
http://cvs.savannah.gnu.org/viewvc/aspell/prog/prezip.c?revision=1.10&root=aspell&view=markup
example:
while (0) {
error_mem_c: retVal = 6; break; /* memory alloc error */
error_out_c: retVal = 5; break; /* output data error */
/* retVal = 3; corrupt input */
}
...however, having a goto pointing to the bottom of a function seems a bit
cleaner and straight to the point.
You shouldn't fear goto statements, just understand them.
If you do gcc and output assembler source for any of the C files, you'll see
a lot of goto statements for all sorts of statements, such as:
if/then/else, case, while, etc...
------------------------------------------------------------------------------
Slashdot TV.
Video for Nerds. Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
fontforge-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/fontforge-devel
http://fontforge.10959.n7.nabble.com/Developer-f3.html