Good code

From 22113
Revision as of 14:22, 6 March 2024 by WikiSysop (talk | contribs) (Created page with "Evaluating code can be done from a number of criteria. In the course we are using two: Correctness and Quality, each have a number of sub criteria. Any evaluation done by teachers will follow this, and when you evaluate your own or your peers code you should use this. === 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.<br> "Natural data sets...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Evaluating code can be done from a number of criteria. In the course we are using two: Correctness and Quality, each have a number of sub criteria. Any evaluation done by teachers will follow this, and when you evaluate your own or your peers code you should use this.

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.