Knowledge Base
Searching in : Article
ID: AR05C00206
Applies to: NX Software
Added on: 2005-05-18
Last Update: 2013-10-09

Why there are goto's in the source file XXX even if they are deprecated

In general it's a good idea to avoid the "goto" statements in C and C++ code. Anyway there are cases, like error handling, where a goto can make the code more efficient and, most importantly, much more readable. For example, you will find many goto's in the Linux kernel and in the X11 code.

From the LKML FAQ:

http://lkml.org/faq/lkmlfaq-15.html

"Admittedly, all those goto's do look a bit ugly. However, they are usually limited to error paths, and are used to reduce the amount of code required to perform cleanup operations. Replacing these with Politically Correct if-then-else blocks would require duplication of cleanup code. So switching to if-then-else blocks might be good Computer Science theory, but using goto's is good Engineering. Since the Linux kernel is one designed to be used, rather than to demonstrate theory, sound engineering principles take priority."

As the article above states, people often propose C and C++ exceptions as an alternative. Besides portability problems, even across compilers, exceptions are goto's incapsulated in high-level abstractions. As all abstractions, they try to hide as much as they can, often with undesired effects, while many programmers want to have full control of what's happening under the hood.