4 ms·
Goto statements are so widely shunned that for a long time, I didn't even realize that C had one. It wasn't until I started reading some Linux code that I found
by tmhedberg 15y ago
Goto statements are so widely shunned that for a long time, I didn't even realize that C had one. It wasn't until I started reading some Linux code that I found out that it existed.
At first I was more than a little bit turned off by the practice, but once I understood _how_ it was being used, it made a lot of sense. The bottom line is, goto statements which jump backwards are generally Considered Harmful. Goto statements which jump forwards, within a limited scope, and used conservatively, can serve to improve the readability of code.
I still almost never use them, but now I know when it would be appropriate to do so, rather than dogmatically rejecting the entire practice. I think the idea that gotos are always bad is one of those things that programmers tend to accept as "received wisdom", without considering the actual motivation for the principle, or in which cases it might not apply as strongly.
- scott_s 15y agoThe Linux kernel does not just use gotos to "jump forwards." If you look careful, they've implemented a simplified version of try-catch-finally. It's a structured use of gotos, implementing clear semantics.
- tmhedberg 15y agoI didn't mean to suggest that Linux kernel gotos were just arbitrary forward jumps. I know that they are used for error handling. That's the legitimate usage I was alluding to, I just wasn't specific about it.