Module 20 · Building Real Python Applications — topic 12 of 13
Improving code quality
Making working code better: refactoring safely with tests, simplifying loops and string building, replacing branch chains with data, finding long functions automatically, and using linters, type checkers and reviews.
Learning objectives
- Refactor code safely behind tests
- Replace complex branching with data-driven code
- Use tools to find quality problems
Definition
Improving code quality means changing code to make it clearer, simpler and easier to maintain without changing what it does (refactoring), supported by tests, automated tools and code review.
Introduction
The expense tracker works. Now is the time to make it pleasant to change — before the next feature makes the messy parts harder to fix.
Why it matters
Quality improvements pay off with every future change: fewer bugs, faster features and code you're proud to show.
Detailed explanation
Refactoring changes structure, not behaviour. Always have tests (or at least comparison checks) that prove the result stays the same.
Work in small steps: rename, extract a function, simplify a condition — then run the tests.
Use Python idioms: iterate directly instead of with indexes, join strings instead of repeated +, use comprehensions for simple transformations.
Replace long if/elif chains that differ only in numbers with a data table (dictionary), so new cases are data, not code.
Measure: very long functions, deep nesting and duplicated blocks are signs that code needs attention. The ast module or linters can find them.
Run linters (ruff), formatters (ruff format, black) and type checkers (mypy) regularly; they find problems humans miss.
Ask for code review: a second reader spots unclear names and missing cases.
Stop when the code is clear; perfect is the enemy of finished.
Syntax
- Same behaviour, better structure.
- Small steps with tests.
- Data instead of branch chains.
Code examples
A simpler version with the same result
True food: 370 travel: 40
Finding long functions automatically
short 2 ok long_one 32 consider splitting
More examples
Replacing a branch chain with data
True 12 110.0
Step-by-step explanation
- The old version repeats the same formula three times with different numbers.
- The new version keeps the numbers in a table and the formula in one place.
- Comparing both on 12 cases shows the refactor preserved behaviour before the old version is deleted.
Expected output
Running “Replacing a branch chain with data” prints exactly:
Practical applications
- Tidying a project before submitting it for review or a portfolio.
- Teams reserving time each sprint to pay down technical debt.
Best practices
- Refactor in small, tested steps.
- Prefer data tables to repeated branches.
- Automate quality checks.
Common mistakes
Refactoring without a safety net
Avoid
Prefer
Without tests, 'cleaner' code may silently behave differently.
Important notes
- Tools such as radon measure cyclomatic complexity, a count of independent paths through a function.
Key takeaways
- Refactoring improves structure without changing behaviour.
- Tests and comparisons prove behaviour is preserved.
- Linters, metrics and reviews guide improvements.
Try it yourself
- 1Pick the longest function in one of your projects and split it into smaller ones, keeping a test passing.
- 2Replace a grading if/elif chain with a data-driven version and prove both agree.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
8 questions cover the “Building Real Python Applications” module. Answers and explanations are revealed after you submit.
- AdvancedWhat does MVP stand for when planning an application?
- AdvancedWhere should input validation ideally happen?
- AdvancedWhat is printed?