Article 3: Code Quality Tools

Introduction

Writing code is one thing; writing code that others (including future you) can read, understand, and maintain is another. Through my experience maintaining codebases over years, I've learned that automated quality tools aren't just nice-to-havesβ€”they're essential for sustainable development.

This article covers the modern Python quality toolkit: Ruff for linting, Black for formatting, and mypy for type checking. Together, they catch bugs early, enforce consistency, and make code reviews focus on logic rather than style.

The Quality Toolchain

spinner
Tool
Purpose
Speed
Configuration

Ruff

Linting + Import sorting

Very Fast

pyproject.toml

Black

Code formatting

Fast

pyproject.toml

mypy

Static type checking

Moderate

pyproject.toml

Ruff: Modern Python Linter

Why Ruff?

Ruff is a Python linter written in Rust that's 10-100x faster than traditional linters. It replaces Flake8, isort, pyupgrade, and many other tools.

# Install Ruff
pip install ruff

# Run linter
ruff check .

# Fix auto-fixable issues
ruff check --fix .

# Format code (like Black)
ruff format .

Configuration

Common Ruff Rules

Running Ruff

Black: The Uncompromising Formatter

Philosophy

Black's design philosophy: "You can have any style you want, as long as it's Black." This eliminates all style debates.

Configuration

Black Formatting Examples

Magic Trailing Comma

Black respects the "magic trailing comma" for multi-line formatting:

mypy: Static Type Checking

Why Type Hints?

Type hints catch bugs before runtime and serve as documentation:

Basic Type Hints

Type Hints in Functions

Configuration

Running mypy

Common mypy Errors and Fixes

Advanced Type Patterns

Integrating All Tools

Combined Configuration

Quality Check Script

Makefile Integration

Pre-commit Integration

VS Code Integration

Practical Exercise

Exercise 1: Fix Code Quality Issues

Create a file with intentional issues and fix them:

Run the quality tools:

Exercise 2: Add Type Hints to Existing Code

Exercise 3: Configure Tools for a Project

Quality Tools Comparison

Aspect
Ruff
Black
mypy

Purpose

Linting + formatting

Formatting only

Type checking

Speed

Very fast (Rust)

Fast

Moderate

Configuration

Extensive

Minimal

Extensive

Auto-fix

Yes

Yes (formatting)

No

When to run

Every save

Every save

Pre-commit/CI

Key Takeaways

  1. Use all three tools together - They complement each other

  2. Configure once, forget it - pyproject.toml centralizes settings

  3. Format on save - Let the tools work automatically

  4. Type hints are documentation - They help you and others understand code

  5. Integrate with pre-commit - Catch issues before they're committed

What's Next?

Quality tools enforce style consistency, but writing truly clean code requires deeper principles. In Article 4: Clean Code Principles, we'll explore naming conventions, function design, and foundational design patterns that make code readable and maintainable.


This article is part of the Software Engineering 101 series.

Last updated