Skip to the content.

Code Quality Standards

Overview

Maintaining high code quality is a priority for the Planwise project. This document outlines our standards and practices for ensuring code readability, maintainability, and reliability.

Style Guides

Python Style Guide

We follow PEP 8 with a few project-specific adjustments:

Documentation Style

Automated Tools

Code Formatting

We use automatic formatters to maintain consistent style:

Static Analysis

We employ multiple static analysis tools:

Pre-commit Hooks

We use pre-commit hooks to automate quality checks before committing:

# .pre-commit-config.yaml
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks:
    -   id: trailing-whitespace
    -   id: end-of-file-fixer
    -   id: check-yaml
    -   id: check-added-large-files

-   repo: https://github.com/psf/black
    rev: 23.1.0
    hooks:
    -   id: black

-   repo: https://github.com/pycqa/isort
    rev: 5.12.0
    hooks:
    -   id: isort

-   repo: https://github.com/pycqa/flake8
    rev: 6.0.0
    hooks:
    -   id: flake8
        additional_dependencies: [flake8-docstrings]

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.0.1
    hooks:
    -   id: mypy
        additional_dependencies: [types-requests, types-PyYAML]

To install pre-commit hooks:

pip install pre-commit
pre-commit install

Code Documentation

Code Comments

Function and Class Documentation

All public APIs should have comprehensive docstrings:

def calculate_recommendation_score(
    preference_score: float, 
    distance: float, 
    popularity: int
) -> float:
    """
    Calculate the final recommendation score for a place.
    
    Args:
        preference_score: User preference score (0-5)
        distance: Distance in meters from user location
        popularity: Number of user ratings
        
    Returns:
        Final recommendation score (0-1)
        
    Raises:
        ValueError: If preference_score is outside the valid range
    """
    # Implementation...

Module Documentation

Each module should have a top-level docstring explaining its purpose:

"""
Recommendation Engine Core
==========================

This module implements the core recommendation algorithms,
including autoencoder-based and SVD-based recommenders.
"""

Testing Standards

Unit Tests

Test Coverage

We aim for at least 80% code coverage:

pytest --cov=src --cov-report=term-missing

Integration Tests

Code Review Checklist

During code reviews, check for:

  1. Functionality: Does the code do what it claims?
  2. Security: Are there any security vulnerabilities?
  3. Performance: Are there any performance concerns?
  4. Error Handling: Is error handling appropriate?
  5. Documentation: Are docstrings and comments clear and complete?
  6. Test Coverage: Are new changes adequately tested?
  7. Style Compliance: Does the code follow our style guidelines?
  8. Compatibility: Are there any backward compatibility issues?
  9. Dependencies: Are new dependencies necessary and appropriate?
  10. Complexity: Is the code unnecessarily complex?

Continuous Integration

Our CI pipeline runs the following checks on every pull request:

Best Practices

Error Handling

Performance Considerations

Security Practices