Techniques for well-designed programs

 

  1. Write out the equations to be solved, their discretized forms, and a basic outline of the code (pseudo code).
  2. Use comment statements – explain what the next block of lines is doing 3. Explicitly define and describe all variables.
  3. Use meaningful variable names. Instead of x, z, use length, width, and height. This is very helpful, both in reading (and rereading) the code, and also for searching for variables.
  4. If you cannot solve your problem, solve an easier problem first.

Helps for Debugging Code

  1. Always output the input to verify that you have the right numbers
  2. Plan for errors:
    • Include intermediate output (not just the final answer)
    • Use simple approaches – not convoluted logic
    • Check answers independently.
  3. “Play computer” line by line, evaluating the logic. This is easier with a debugger 4. Explain your logic to someone else (or yourself out loud).
  4. Be aware of roundoff errors (just use double precision)
  5. Check for:
    • mathematical hierarchy,
    • integer division,
    • correct array dimensions, transferring arrays from subroutines,

These guidelines will slow you down for very simple codes, but in the long run, they will save you a lot of time and effort if you are involved in real code development.