Zen Of Fortran

Introduction

I have been writing Fortran programs for half a year. Most of codes I read and write are based on F77 and simple F90 standard. It’s important to form a programming style. I find a web listing guide line for Fortran poor people. I paste the Zen of Fortran here and write down some beautiful coding style for futher use.

Zen of Fortran

  1. standard compliance is better than custom or extended;
  2. beautiful is better than ugly;
    1. readability counts;
    2. explicit is better than impl.;
    3. simple is better than CoMpleX;
    4. CoMpleX is better than c0mp1|c@ted;
    5. flat is better than nested;
    6. s p a r s e is better than dense;
  3. fast is better than slow:
    1. vector is better than loop;
    2. matrix is better than vector;
    3. strided is better than scattered;
    4. contiguous is better than strided;
    5. broadcasting is a great idea, use where possible;
  4. slow is better than unmaintainable;
  5. make it look like the math;
  6. special cases aren’t special enough to break rules…
  7. although practicality beats purity;
  8. pure procedure is better than impure
  9. although practicality beats purity again;
  10. private is better than public;
  11. errors should never pass silently
  12. unless errors are explicitly silenced;
  13. to be continued

References

This list is inspired by many sources:

[1] zen of python.

[2] Fortran 2003 standard.

[3] Modern Fortran: Style and Usage, a book by Norman S. Clerman and Walter Spector.

[4] The Fortran Company styles.

[5] Fortran best practices.

[6] European Standards For Writing and Documenting Exchangeable Fortran 90 Code.

[7] Fortran Coding Standard for the Community Atmospheric Model.

Go to TOP

Guideline

readability

Template

Some software (eg. Doxygen) can generate a documentation of codes automatically. So I paste some template here for future use.
Prologue for Functions and Subroutines

!-----------------------------------------------------------------------
! BOP
!
! !ROUTINE:  <Function name>  (!IROUTINE if the function is in a module)
!
! !INTERFACE:  
            function <name> (<arguments>)          

! !USES:
            use <module>

! !RETURN VALUE:
            implicit none
            <type> :: <name>               ! <Return value description>

! !PARAMETERS:   
            <type, intent> :: <parameter>  ! <Parameter description>              

! !DESCRIPTION:
! <Describe the function of the routine and algorithm(s) used in
! the routine.  Include any applicable external references.>
!
! !REVISION HISTORY:
! YY.MM.DD  <Name> <Description of activity>
!
! EOP
!-----------------------------------------------------------------------
! $Id: code_conv_cam.tex,v 1.3 2001/06/19 21:44:14 kauff Exp $
! $Author: kauff $
!-----------------------------------------------------------------------

Prologue for a Module

!-----------------------------------------------------------------------
! BOP
!
! !MODULE:  <Module name>
!
! !USES:
           use <module>
! !PUBLIC TYPES:
            implicit none
            [save]

            <type declaration>

! !PUBLIC MEMBER FUNCTIONS:
!           <function>                     ! Description      
!
! !PUBLIC DATA MEMBERS:
            <type> :: <variable>           ! Variable description

! !DESCRIPTION:
! <Describe the function of the module.>
!
! !REVISION HISTORY:
! YY.MM.DD  <Name> <Description of activity>
!
! EOP
!-----------------------------------------------------------------------
! $Id: code_conv_cam.tex,v 1.3 2001/06/19 21:44:14 kauff Exp $
! $Author: kauff $
!-----------------------------------------------------------------------

Prologue for a Header File

!-----------------------------------------------------------------------
! BOP
!
! !INCLUDE:  <Header file name>
!
! !DEFINED PARAMETERS:
            <type> :: <parameter>          ! Parameter description

! !DESCRIPTION:
! <Describe the contents of the header file.>
!
! !REVISION HISTORY:
! YY.MM.DD  <Name> <Description of activity>
!
! EOP
!-----------------------------------------------------------------------
! $Id: code_conv_cam.tex,v 1.3 2001/06/19 21:44:14 kauff Exp $
! $Author: kauff $
!-----------------------------------------------------------------------

More

In the future, I decide to use C++ instead of Fortran. But there is a lot to do if I want to write beautiful codes.

More to be writen.