Skip to content

Clean Code

Writing clean, readable, and maintainable code is crucial for reproducible research.

This section covers Python coding standards, linting, and formatting tools to help you write high-quality, consistent code.

Overview

Clean code practices ensure that your research code is:

  • Readable: Easy to understand by you and your collaborators
  • Maintainable: Simple to modify and extend
  • Reproducible: Consistent results across different environments
  • Professional: Follows industry standards and best practices

What You'll Learn

This guide covers:

  1. Python Standards: Understanding PEPs and core Python conventions
  2. Style Guidelines: PEP 8 and formatting best practices
  3. Linting and Formatting: Modern tools like Ruff for code quality
  4. Type Hints: Using Python's type system for better code
  5. Documentation: Writing effective comments and docstrings
  6. Best Practices: Additional principles for clean code
  7. Workflow Integration: Automating code quality in your development process

Quick Start

For immediate improvement to your code quality:

  1. Install Ruff:
pip install ruff
conda install -c conda-forge ruff
# or
mamba install -c conda-forge ruff
uv add ruff
poetry add ruff --group dev
  1. Run linting: ruff check .
  2. Auto-format: ruff format .
  3. Add type hints to your functions
  4. Write docstrings for your modules and functions

By following these guidelines and using modern tools, you'll produce cleaner, more maintainable, and more reproducible code.