Introduction
Introduction
Code that works once isn't enough. Code that keeps working, and that others can read and change, comes from deliberate habits.
Defensive programming means writing code that expects problems before they happen. You check inputs, handle errors, and document what your code does.
Think of it like a house. Locks and smoke detectors aren't the flashy parts, but they keep the house safe. This module teaches four practices:
- Write readable names and use a consistent layout.
- Document functions with docstrings and type hints.
- Validate input and handle exceptions.
- Test functions with
assertandunittest.
After you learn the four practices, you apply them together in an exercise that refactors a sales report program.
Start with readability.