General Principles
Code Readability
Write code that is easy to read and understand. Use descriptive names, consistent formatting, and clear logic. Favor explicitness over cleverness.
Consistency
Follow established conventions for naming, structure, and formatting. Consistent code is easier to maintain, review, and extend.
Simplicity
Keep code as simple as possible. Avoid unnecessary complexity, deep nesting, and over-engineering. Refactor complex logic into smaller, reusable functions or classes.
DRY (Don't Repeat Yourself)
Avoid duplicating code. Use reusable functions, classes, and templates to keep the codebase maintainable.
Separation of Concerns
Organize code so that each module, class, or function has a single responsibility. Keep business logic out of views and templates; use models and services for data and logic.
Example
# Good: Clear, simple, and maintainable
class InvoiceCalculator:
def calculate_total(self, items):
return sum(item.price for item in items)
# Bad: Unclear and complex
class InvoiceCalculator:
def calc(self, x):
t = 0
for i in x:
t += i.p
return t