Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think part of the problem is that everyone heard that goto is harmful but noone ever reads the original letter, even though its reads like a short blog post (assuming they had blogs back then)

http://www.u.arizona.edu/~rubinson/copyright_violations/Go_T...

According to Dijkstra, the really evil gotos are the ones that force you to keep a track of the whole execution path in order to figure out the current state of the program. Its much easier when you can look at a line of code and statically know what the program state will be like at that point.

A for loop is better than goto because you can look at a line inside it and instantly know that it will run a number of times, with the index changing in increasing order.

gotos for resource cleanup are good because you can look at a given line of code and know what resources still need to be freed.

Code that gets rid of gotos and break statements by blindly replacing them with tons of flags is just as bad as gotos because you still need to look at the whole execution path to figure out the state in the flags.



Relevant quote from "GO TO statement considered harmful": "The remark about the undesirability of the go to statement is far from new. I remember having read the explicit recommendation to restrict the use of the go to statement to alarm exits, but I have not been able to trace it; presumably, it has been made by C. A. R. Hoare."

He doesn't explicitly say it, but it seems he agreed with that use of go to.


> I think part of the problem is that everyone heard that goto is harmful but noone ever reads the original letter

Same thing for the premature optimization quote from Knuth.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: