4 ms·
In short, it's not a bad practice in all cases and, like most issues, it depends. The "goto considered harmful" article by Dijkstra is more nuanced (and that w
by cataflam 13y ago
In short, it's not a bad practice in all cases and, like most issues, it depends.
The "goto considered harmful" article by Dijkstra is more nuanced (and that wasn't even its intended title) and doesn't really address the way the goto you are seeing are used.
They are a very common pattern in C, for error checking and cleanup in case of error. It's much more readable than to nest conditional statements.
if (some_parameter > max_valid_value)
goto error;
if (some_function() <= )
goto error;
...
...
if (some_alloc() == NULL)
goto error;
...
...
...
...
error:
// cleanup what needs to be freed,...
It can also be quite useful to exit nested loops for example. It's commonly used in low-level C.
However, when you start goto'ing backwards instead of always forwards, it becomes terrible for readability, so it is avoided (well, most of the time).
For more reading on the subject, this excerpt from Code Complete by Steve McConnell discusses the subject further :
http://www.stevemcconnell.com/ccgoto.htm http://www.stevemcconnell.com/ccgoto.htm