Uwe Hermann uwe@hermann-uwe.de writes:
On Fri, Nov 17, 2006 at 01:12:55PM +0100, Stefan Reinauer wrote:
The initial idea is that the use of goto makes it hard for the compiler to do code flow optimization.
The initial idea why you should _not_ use GOTOs is that GOTOs suck ;-) There was a paper called "Go To Statement Considered Harmful" from Edsger W. Dijkstra (and that man probably knows what he's talking about) in 1968 (!). GOTOs usually produce unreadable, unmaintainable code and should generally not be used.
But yeah, in low-level programming it may make sense in certain situations, as the code sometimes gets very ugly otherwise...
To a certain extent the title of that paper is unfortunate. What Dijkstra was arguing for was single points of entry and single points of exit. This makes proofs (and other kinds of understanding) of the code much easier.
So the argument was against multiple return statements, continue and break, every much as it was against goto. The core observation was that you can replace any control dependency and with a data dependency and get easier to follow code. (i.e. you can introduce a variable).
Stefan point with the compilers is also valid because they have to understand the code they like this style of programming.
So the ideal is a single point of entry and a single exit from every block of code. In general using gotos for exception handling improve this situation because they reduce the number of unique exit paths from a piece of code. In that context goto's also leave the code more readable because you don't get lots of extra unnecessary levels of indentation, and it easier to follow what the code is really trying to do. So if you are not doing a formal proof but a human inspection proof the goto certainly helps.
I actually suspect Dijkstra might argue against C++ exception handling due to the challenges it places on proving code is correct. The control flow can be completely non-obvious when exceptions are involved. I know there is a lot of C++ that is proof and compilation hostile which makes it a poor choice for low level systems programming.
Dijkstra's papers are available online and I challenge any one who wants to understand this better to dig them up and read them.
Eric