WK2: Coding Standards and Hands-on Exercises (Session 2)

Welcome
Coding Standards and Hands-on Exercises
Module Lecturer: Dr Raghav Kovvuri
Email: raghav.kovvuri@ieg.ac.uk

Week 2
1 / 11
volgende
Slide 1: Tekstslide
ProgrammingHigher Education (degree)

In deze les zitten 11 slides, met tekstslides.

Onderdelen in deze les

Welcome
Coding Standards and Hands-on Exercises
Module Lecturer: Dr Raghav Kovvuri
Email: raghav.kovvuri@ieg.ac.uk

Week 2

Slide 1 - Tekstslide

Coding Standards Introduction
Definition: A set of guidelines for a specific programming language that recommend programming style, practices, and methods
Importance:
  • Improves code readability
  • Facilitates maintenance
  • Helps prevent errors
  • Promotes consistency across projects and teams
Python's primary coding standard: PEP 8 (Python Enhancement Proposal 8)
Tools for enforcement: pylint, flake8, black (auto-formatter)

Slide 2 - Tekstslide

PEP 8 - Key Guidelines (1)
Indentation:
  • Use 4 spaces per indentation level
Maximum Line Length:
  • Limit lines to 79 characters for code
  • Limit docstrings/comments to 72 characters
Imports:
  • One import per line
  • Group imports: Standard Library, Third-Party, Local
  • Avoid wildcard imports (from module import *)

Slide 3 - Tekstslide

PEP 8 - Key Guidelines (2)
Naming Conventions:
  • Functions, variables, and attributes: lowercase_with_underscores
  • Classes: CapitalizedWords
  • Constants: ALL_CAPS
  • Protected instance attributes: _single_leading_underscore
  • Private instance attributes: __double_leading_underscore
Whitespace:
  • Surround top-level functions and classes with two blank lines
  • Use blank lines sparingly inside functions to indicate logical sections

Slide 4 - Tekstslide

PEP 8 - Key Guidelines (3)
Comments:
  • Use complete sentences, starting with a capital letter
  • Use inline comments sparingly
  • Update comments when code changes
Documentation Strings:
  • Enclose in triple quotes: """
  • First line should be a summary
  • Multi-line docstrings: summary line, blank line, details

Slide 5 - Tekstslide

Hands-on Exercise 1
Code Refactoring:

Task: Refactor the code given (download from Canvas) to adhere to PEP 8 standards
Key points to address:
  • Function naming
  • Whitespace in expressions
  • Docstrings
  • Line breaks and indentation
timer
30:00

Slide 6 - Tekstslide

Hands-on Exercise 2
Task: Create a simple to-do list program that demonstrates good coding practices and adherence to PEP 8.
Requirements:
1) Use a list to store to-do items
2) Implement functions for:
  • Adding an item
  • Removing an item
  • Displaying the list
3) Use a main loop for user interaction
4) Include appropriate comments and docstrings
5) Follow PEP 8 guidelines for naming and formatting

Slide 7 - Tekstslide

Best Practices for Clean Code
1) DRY (Don't Repeat Yourself)
  • Avoid duplicating code; use functions or loops instead
2) KISS (Keep It Simple, Stupid)
  • Write simple, straightforward code
  • Avoid unnecessary complexity
3) YAGNI (You Aren't Gonna Need It)
  • Don't add functionality until it's necessary
4) Single Responsibility Principle
  • Each function or class should have one, well-defined purpose
5) Meaningful Names
  • Choose descriptive names for variables, functions, and classes
6) Error Handling
  • Use try-except blocks to handle exceptions gracefully
7) Code Organization:
  • Group related code together
  • Use modules to separate concerns

Slide 8 - Tekstslide

Tools for Maintaining Code Quality
Linters:
  • pylint: Checks for errors and enforces a coding standard
  • flake8: Combines PyFlakes, pycodestyle, and Ned Batchelder's McCabe script
Formatters:
  • black: An opinionated code formatter; "any style guide is better than no style guide"
  • YAPF: Formatter from Google, highly configurable
Type Checkers:
  • mypy: Optional static type checker for Python
Documentation Generators:
  • Sphinx: Generates documentation from Python docstrings
  • pdoc: Auto-generates API documentation for Python modules
Testing Frameworks:
  • unittest: Built-in unit testing framework
  • pytest: More feature-rich testing framework

Slide 9 - Tekstslide

Recap and next session
Further Practice:
1) Refactor previous exercises to follow PEP 8
2) Use pylint or flake8 to check your code
3) Try using black to automatically format your code
4) Start a small project, focusing on clean code from the beginning

Next Session Preview:
  • Introduction to Object-Oriented Programming
  • How OOP principles can further improve code organization and reusability

Slide 10 - Tekstslide

Slide 11 - Tekstslide