星期五, 10月 05, 2007

Programming Styles

Goal:
Make your code easy to read and easy to modify.

Spelling conventions
  • name the variables as the names suggest
  • Start the name of classes with uppercase letters (such as String, FirstProgram) and start the names of variables, objects, and methods with lowercase letters.
  • Word boundaries are indicated by an uppercase letter, as in numberOfPods
  • Java spells things out in full. Use BufferedReader instead of BufReader, BuffReader, BufferedR or BR. With abbreviations, we often cannot recall how the identifier is abbreviated.
Comments
  • "Just enough" is enough.
  • When something is important and not obvious, it merits a comment.
  • Example of unnecessary comment
interest = balance * rate; //Computes the interest.

Self-documenting
  • the structure of the program is clear from the choice of identifier names and indenting pattern.
Indenting
  • The general rule of indenting is easy. When one structure is nested inside another structure, the inside structure is indented one more level.
  • We prefer to use four spaces for each level of indenting. Indenting only two or three spaces is fine so long as you are consistent. One space is not enough to be clearly visible.

沒有留言: