Good code

From 22101
Jump to navigation Jump to search

Good code is both Correct and have good Quality.

Correctness

This is simply to which degree does the solution(s) give the right answer when using the data set supplied with the exercise - or similar natural data sets.
"Natural data sets" in this context are data set, that

  • are NOT specially constructed to catch minor flaws in the programming.
  • do NOT differ significantly from what has been used in exercises.
  • are NOT unlikely to occur in Real Life.

Quality

The quality of the code is reflected in a lot of properties.

  • Easy to read
  • Easy to maintain
  • Easy to extend
  • Easy to use
  • Explicit code
  • Comments explaining the code - what is happening. Usually 10-20% of the chars used should be comments. There is such thing as too many comments - when you have more comments than code you are over the limit.
  • Well-chosen placement of comments. They should not disrupt the reading of the code.
  • Well-chosen descriptive names for variables, subroutines, etc.
  • Modular construction of the code
  • No special cases, but general programming
  • Not bloated code - slim and elegant
  • Demonstrating overview of the problem
  • Avoiding using break and sys.exit in ways that break the natural flow of the code
  • Catches errors in the input - and explains what the error is
  • Code is optimized and logical
  • Code does not unnecessarily use memory - like slurping files for no good reason
  • Using correct data structures and understanding the basis of the various types
  • Using efficient and clear algorithms
  • Correct use of Python idioms, concepts and constructs

Many of these properties overlap to a lesser or greater extent. It does not matter. What matters is that they explain in different ways what code quality is. The teachers solutions are (mostly) examples of good code. Often various solutions to the same problem are shown and pro's and con's of each discussed - read them.

  • Online: Clean Code by Lukasz Dynowski. This resource gives a lot of practical advice on how to make good code.