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
- standard compliance is better than custom or extended;
- beautiful is better than ugly;
- readability counts;
- explicit is better than impl.;
- simple is better than CoMpleX;
- CoMpleX is better than c0mp1|c@ted;
- flat is better than nested;
- s p a r s e is better than dense;
- fast is better than slow:
- vector is better than loop;
- matrix is better than vector;
- strided is better than scattered;
- contiguous is better than strided;
- broadcasting is a great idea, use where possible;
- slow is better than unmaintainable;
- make it look like the math;
- special cases aren’t special enough to break rules…
- although practicality beats purity;
- pure procedure is better than impure…
- although practicality beats purity again;
- private is better than public;
- errors should never pass silently…
- unless errors are explicitly silenced;
- to be continued
References
This list is inspired by many sources:
[1] zen of python.
[3] Modern Fortran: Style and Usage, a book by Norman S. Clerman and Walter Spector.
[4] The Fortran Company styles.
[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.