Write Readable Code


  • Clarity: Make sure your code is easy to understand.
  • Simplicity: Keep the code simple and avoid unnecessary complexity.
  • Structure: Organize your code well (e.g., use functions, classes, and modules appropriately).
  • Minimal Redundancy: Avoid code duplication to improve efficiency.
  • Documentation: Add comments to clarify the logic and intent behind your code.

Write Elegant Code


  • Naming variables is an important step to improve code readability.
  • A good name can explain the intent of the code without extra information.
  • Some guidelines can help name variables, functions, and modules.
  • Avoid single letters, ambiguous names and magic numbers in your code
  • Be consistent.

Write Robust Code


  • Docstrings provide clear documentation of your code and improve its readability and usability.
  • Error handling techniques help build robust code. The idea is to anticipate potential errors and to implement mechanisms to handle them gracefully.
    • Assertions ensure that certain conditions are met during code execution.
    • By raising specific exceptions (e.g. TypeError, ValueError), you can not only control the behaviour of your code when things go wrong but also inform the user that something is right.

Write Reliable Code


  • Testing increases trust in code and its results.
  • Test Driven Development helps to write tests alongside code
  • Pytest offers a powerful testing framework from Python code, offering different built-in functions and methods to test various aspects of the code:
    • @pytest.mark.parametrize: To test different input values
    • pytest.raises: To test error handling