Do’s and Don’ts of Style

A great programmer makes their code clear, robust, efficient, and user-friendly.
Code needs to be clear so others can read and understand your code.
Code needs to be robust* to reduce the number of errors in your program.
Code needs to be efficient to reduce the cost of your code.
Code needs to be user-friendly so that people will want to use your program.

Here are some Do’s and Don’ts that will guide you to becoming a better programmer. Bad style will cost your grade so keep these in mind and think of your own as well.

DO:

  • Comment your code concisely
  • Give your variables and functions good names for clarity
  • End your phrases with semi-colons
  • Indent your code properly
  • Include your name, andrew ID, and section on the top of your code

DON’T:

  • Have more than 80 characters in a line.
  • Have dead code (code that does nothing, or commented out code)
  • Use meaningless variable names
  • Use magic numbers (numbers that come out of nowhere)
  • Add too many comments, or redundant comments.

Here is a comparison of good and terrible code. If you can’t differentiate the two, ask for assistance as soon as possible.

Screen-Shot-2015-09-06-at-11

Screen-Shot-2015-09-06-at-11_002

*“Robust” is a great adjective that, when talking about software, has a fairly specific and technical meaning. Robust software is software that works without depending on many assumptions or conditions. A program that crashes when an expected file is not found is not robust. A program that reports the missing file, prompts the user to find the file, or presses on using reasonable default values is more robust.