Unread post Wed Sep 17, 2008 5:34 am

Good programming practices

These programming practices are compiled from the the PowerPoint files you submitted for Homework 1. Please add to this list for the sake of your classmates if you think of or discover another good practice.

  • Give variables meaningful names.
  • Develop a consistent system of capitalization for variable names and stick with it.
  • Keep the names of constants in all capital letters.
  • Free allocated memory once it is no longer needed.
  • Once memory has been freed, do not access it again.
  • Use indentation to indicate scope.
  • Use comments to explain all functions and variables as well as the flow of the driver program.
  • At the top of every program, use a comment to explain what the program does.
  • Write these comments before and while you are writing the code, not a long time afterward.
  • Break down decision statements so that the logic is clear.
  • Divide the code into functions whenever possible to assist in debugging and enhance reusability.
  • Avoid hardcoding inputs to a program to enhance its flexibility. Instead, take input from an argument or prompt the user.
  • Before using these inputs, check that they are valid.
  • The first time around, write your algorithm "the simple way". Later, if the program works, you can try to optimize it.
  • If you get stuck, or become frustrated, take a break and work on something else. Come back to the problem later with a clear mind.
  • Try to keep your code as readable and organized as possible. Eliminate redundant or unused code.
  • Making liberal use of whitespace, both horizontally and vertically, can make your program much easier to read.
  • Limit each line to 80 characters.
  • When commenting out large pieces of code for debugging purposes, use the format "/* ...... /**/" so that the comment can be added and removed easily.