Good programming practice

Ulf Ryde, 050301

This is a set of advices for the members of our group that program. The aim is to make our programs more similar and make it possible to other people to read and modify your programs after you have left the group. Moreover, I beleive that if you follow these rules, your programs will be more efficient and more easily debugged.
The advices are written for Fortran, but mostly applies also to other languages.
  1. Always use
    implicit none
  2. Mark each subroutine clearly.
    Start each subroutine with a short description what is none, what input is neede and what output is given.
    Also write who has written the subroutine and when.
  3. Divide your subroutins into a number of short and well-defined tasks (~10 program lines). Start each task with a short (~1 line) comment of what is done.
    Divide the tasks by a single empty line
  4. Indent your file (emacs does that automatically).
  5. Keep the 72 character lines.
  6. Do not use continue but enddo
  7. Let array dimensions always be named parameters, not explicit numbers (except for the three Cartesian dimensions).
  8. Let file numbers be named variables, not explicit numbers.
  9. Always use read*8 (=double precision)
  10. Never use write(*,*) - it does not work in parallel; use write(6,*) instead.
  11. Use long and instructive names for all variables (except do-loop indeces).
  12. Always initialise all variables (even with 0).
  13. Always remember to give .0d0 for all real constants (including 0.0d0, 1.0d0 and 2.0d0)
  14. Make all scripts as general as possible: Assume that the programs are in your PATH and do not include an explicite path.
  15. Always rename old versions of programs, not the new ones (the current preferred version should always have the simplest and most obvious name).
Utility files - see below

Sample file

****|****1****|****2****|****3****|****4****|****5****|****6****|****7**
      Subroutine FndXYZMax(natom,nresi,cresnr,resnr,residu,atom,xyz
     &     ,imax,imin)
c     For each dimension: x,y,z, the maximum and minimum coordinates
c     are found
c     The corresponding atom numbers are returned as imax and imin
c     Ulf Ryde, 28/5-02; 2/10-04

      implicit none
      integer natom,nresi,cresnr(natom),resnr(nresi),imax(3),imin(3)
      character*(*) atom(natom),residu(nresi)
      double precision xyz(3,natom)

c     Local
      integer i,j
      double precision xmax(3),xmin(3)

c     Initialise
      do i=1,3
         imax(i)=0
         imin(i)=0
         xmax(i)=-1.0d100
         xmin(i)=1.0d100
      enddo

c     For each coordinate find the maximum and minimum coordinate    
      do i=1,natom
         do j=1,3
            if (xyz(j,i).gt.xmax(j)) then
               imax(j)=i
               xmax(j)=xyz(j,i)
            endif
            if (xyz(j,i).lt.xmin(j)) then
               imin(j)=i
               xmin(j)=xyz(j,i)
            endif           
         enddo
      enddo 
      end
****|****1****|****2****|****3****|****4****|****5****|****6****|****7**

Utility subroutines

We have collected a large number of utility routines in Fortan for manipulating our standard files. These are found in /home/bio/Utilib.
Please, use these as much as possible.

They are:
amber.f      - routines for manipulating Amber files
bio.f        - routines for finding H-bonds and hydrophobic interactions
cns.f        - routines for manipulating CNS files
comqum.f     - routines for manipulating ComQum files
connect.f    - routines for construction connect lists between atoms
convert.f    - routines for converting data
eqnsolve.f   - routines for solving 2nd, 3rd, and 4th degree equations
files.f      - routines for manipulating files
findproc.f   - routines for finding and sorting in arrays
fit.f        - routines for charge fit
fndbon.f     - routines for finding bonds between atoms
gaussian.f   - routines for manipulating Gaussian files
geoproc.f    - routines for doing various geometric calculations
infile.f     - routines for manipulating Mumod infiles
matrixproc.f - routines for manipulating matrices
mead.f       - routines for manipulating Mead files
molcas.f     - routines for manipulating Molcas files
moments.f    - routines for manipulating electrostatic moments
mopac.f      - routines for manipulating Mpoac files
mulliken.f   - routines for manipulating Mulliken files
pdb.f        - routines for manipulating pdb files
rdgeo.f      - routines for manipulating geo files (from describe)
selatm.f     - routines for selecting atoms
seminario.f  - routines for converting a Hessian matrix to force constants of internal coordinates
shortcon.f   - routines for calculating short contacts between atoms
spartan.f    - routines for manipulating Spartan files
stringproc.f - routines for manipulating strings
termodyn.f   - routines for calculating thermodyanic properties from frequencies
turbomole.f  - routines for manipulating Turbomole files

Sorted after function:

Manipulating files
amber.f      - routines for manipulating Amber files
cns.f        - routines for manipulating CNS files
comqum.f     - routines for manipulating ComQum files
gaussian.f   - routines for manipulating Gaussian files
infile.f     - routines for manipulating Mumod infiles
mead.f       - routines for manipulating Mead files
molcas.f     - routines for manipulating Molcas files
moments.f    - routines for manipulating electrostatic moments
mopac.f      - routines for manipulating Mpoac files
mulliken.f   - routines for manipulating Mulliken files
pdb.f        - routines for manipulating pdb files
rdgeo.f      - routines for manipulating geo files (from describe)
spartan.f    - routines for manipulating Spartan files
turbomole.f  - routines for manipulating Turbomole files

Manipulating certain data types
convert.f    - routines for converting data
files.f      - routines for manipulating files
geoproc.f    - routines for doing various geometric calculations
matrixproc.f - routines for manipulating matrices
stringproc.f - routines for manipulating strings

Performing certain calculations
findproc.f   - routines for finding and sorting in arrays
eqnsolve.f   - routines for solving 2nd, 3rd, and 4th degree equations
fit.f        - routines for charge fit
seminario.f  - routines for converting a Hessian matrix to force constants of internal coordinates
termodyn.f   - routines for calculating thermodyanic properties from frequencies

Performing atoms-in-protein manipulations
bio.f        - routines for finding H-bonds and hydrophobic interactions
connect.f    - routines for construction connect lists between atoms
fndbon.f     - routines for finding bonds between atoms
selatm.f     - routines for selecting atoms
shortcon.f   - routines for calculating short contacts between atoms