Clean code is code that another developer can read and change without pain. It matters more than clever tricks, especially when you work in a team.
Names that explain themselves
Use names that say what a variable holds, such as monthly_revenue instead of x. A function name should describe the action, such as calculate_discount.
Small functions
A function should do one job. If you need a comment to explain what a block does, it may be a function of its own.
Avoid repetition
If you copy a block of code a third time, move it into a function. Repeated code is where bugs hide.
Format and check
- Use a formatter such as Black, so the style is the same across files.
- Use a linter such as flake8 to find unused variables and mistakes.
- Write one test for each important function.
Next step: see our Python Full Stack course, with code review in every project at ScholarLogic, or book a free demo class. A counsellor will share the fee and the next batch dates.
