Go To Statement Considered Harmful: Fortran examples

I can remember a time when this was unthinkable. These days, with 4+ GHz processors and 32-bit default integers, it is not an unreasonable situation.

I personally like the unindexed DO…ENDDO structure. When I see things like DO WHILE (.true.)…ENDDO in other languages, I think it looks like an inelegant hack around a missing language feature.

I’m unsure about the idea of a BLOCK structure hiding as a loop. It seems to me if you have a loop, which is a fundamental programming concept, it should be unambiguous that it is a loop.

:100: If you want to go back to the beginning of a block of code, that’s a loop, which in Fortran is spelled do.

3 Likes

I agree. It is spelled DO, not BLOCK. that is what I meant, that the looping structure should be obvious.

1 Like

Don’t forget to use the following hidden GOTOs on your highway to Hell, it could probably be useful for a better entanglement. This example comes from the Fortran 2018 standard draft (I have just modified the err value):

Figure 10.3 Testing for an error or the end of the file.

read (nunit, ’(3f10.3)’, iostat=ios, err=666, end=120) a,b,c 
! Successful read - continue execution. 
... 
666 call error (ios) ! Error condition - take appropriate action. 
return 
120 call last_file ! End of file - test for more files.

I am always surprised when I am compelled to use such error labels in modern Fortran. Like meeting Cthulhu…

3 Likes

It’s funny you mention Cthulhu :laughing:; the monster also appears on the cover of a C book: Effective C | No Starch Press

image

2 Likes

Well, a monstrous thing of the past with tentacles :octopus:, isn’t it GOTO and spaghetti code :spaghetti: ?

When implementing them in stdlib , there were some discussions about the algorithms (see e.g. this issue). some references can be also found at the end of this page.

1 Like

Octopuses at least are pretty intelligent and nifty, just like goto. :wink:

Anyways, to deepen the discussion on goto’s, it appears this is a quite big topic in decompilation and malware analysis. I’ve found several interesting academic works on the topic:

The SPAG code restructuring tool has some amazing looking examples of automatic goto transformation. This doesn’t mean you’ll understand the code purpose, but at least it’s much more readable and easy to track the flow. I wonder how complex the algorithms that SPAG uses are, and if they were developed independently.

I imagine it started similar to the xkcd comic:

2 Likes

If someone wants to follow up, Simulating Normal Random Variables – Quant Girl has nice explanations.

1 Like