NERSC logo National Energy Research Scientific Computing Center
  A DOE Office of Science User Facility
  at Lawrence Berkeley National Laboratory
 

Good Programming Practices

The best debugging strategy is to use good programming practices, since this is the key to easy debugging. Obvious practices include:
  • commenting your codes
  • including line breaks for ease of reading
  • keeping up to date variable lists
  • using revision control
  • indenting loops and IF blocks
  • using IMPLICIT NONE
  • selecting variable and file names that suggest what they represent

It is also a good idea to try and align complicated equations to easily catch syntax and typing errors. For example, both these statements have the same typo, but it is readily apparent in the well-written one:

example =   ( ax1*by1 - ay1*bx1 )  &
          - ( ax1*bz1 - az*bx1  )  &
          + ( ay1*bz1 - az1*by1 )   


example=(ax1*by1-ay1*bx1)-(ax1*bz1-az*bx1)+(ay1*bz1-az1*by1)

These kinds of typos can be difficult to track down, because if variable az exists, the compiler won't catch the error even when you use IMPLICIT NONE. You have to wait until wrong answers show up in your code to realize you made the typo. If the variable az is close enough in value to az1, it will take a while.

Another programming practice that can be helpful but many people don't use is the make utility. There is a good NERSC make tutorial which gives a general introduction on using make. Advanced programmers aren't the only ones who should use make; it is useful for smaller codes that only have a few source files as well. For instance, it is an excellent way to keep track of the various compiler options used.


LBNL Home
Page last modified: Fri, 21 May 2004 20:47:44 GMT
Page URL: http://www.nersc.gov/nusers/help/tutorials/debug/practice.php
Web contact: webmaster@nersc.gov
Computing questions: consult@nersc.gov

Privacy and Security Notice
DOE Office of Science