Is there a go-to code formatter for Fortran?

You go way beyond formatting, as does spag and LFortran in their ways as well. If I were to define formatting as changing the appearance of the code but not the restructuring of it I think common ones are changing .EQ. to == and vice-versa comes up a lot; on the farther edge is changing all the “end if” to “endif” and vice-versa and related changes – “go to” versus “goto” …; moving DATA declarations to above the executable statements; moving FORMAT statements to a group at the bottom or converting them all to CHARACTER parameters and using FMT=NAME instead of FMT=NUMBER (which I personally prefer myself but which seems implemented more rarely).

With longer line lengths allowed now it is sometimes preferable to join continued lines back into a single line, particularly if strings or (worse yet) variable names were split across lines.

I think (f) is covering changing REAL*8 to real(kind=real64) and maybe worse in some respects is real(8). REAL*8 either fails or gives you the expected behavior, but real(8) can point to the wrong kind. I see a big split in who likes the KIND= included. Some prefer real(int32) and others real(kind=int32); putting the :: into
declarations as in “real :: A” versus “real A”; and lining up the ::; putting all variables with like attributes in a single declaration versus one declaration per variable seems to be a big point of division.

The situation is that there is a big variation in preferences so the best tool lets every organization easily convert imported code they need to maintain to their favorite style.

That leads to comments being compatible with ford and doxygen. Some like all comments to start on a line by themselves beginning at the left margin; others like the beginning exclamation to line up with the beginning of the code below it; others prefer in-line comments on the ends of the statements; and some like those lined up and significantly spaced away from the end of the code.

Another twist on comments is whether they are all contained between the procedure statement and end or whether a description precedes any of the code,
but that would seem to be hard to automate recognizing.

I prefer all procedures to end with their names, like “end function myfunc”; others prefer a simple “end”. Same with interface blocks, etc. Some people like to name every DO loop and IF/ELSE/ENDIF; some like that just where it adds clarity to the code.

The one thing that is the same is that everyone’s preferences seem to be different.

PS: Mentioning “GO-TO” and Fortran in the same breath brings up bad images, I am afraid. Maybe “preferred” or “standard” ? :slight_smile:

1 Like