Below is a quick standards markdown notes I wrote for a codebase I was managing recently.
This document outlines the coding standards, comment guidelines, and best practices to be followed by all contributors to maintain code quality and consistency across the project.
In this project, we stick to the PEP 8 style guide for Python code to ensure readability and consistency. All Python code should follow these guidelines:
ms-python.autopep8
.To commit your changes, use the following steps based on your operating system:
Unix Systems:
Make the script executable (only needed once):
chmod +x gitupdate.sh
Run the script with your commit message:
./gitupdate.sh "COMMIT_MESSAGE"
Windows:
Simply run:
gitupdate.bat
Each commit should be linked to an issue to ensure traceability in the development process. A developer should fork the repository and create a new branch for each issue they work on. When committing changes, reference the relevant issue number in the commit message. To automatically close an issue when the commit is merged, use the keywords “Fixes,” “Closes,” or “Resolves” followed by the issue number. For example:
git commit -m "Feature: Add user login functionality (Closes #123)".
Some prefix we might use:
Don’t forger to document every modification you make.
We should organize import statements in a structured manner to enhance code readability and maintainability. Begin with standard library imports, followed by third-party library imports, and finally your project-specific imports. Group imports logically and separate them with a blank line to improve clarity. The recommended order is:
import html
, import json
)from django.db.models import Q
, from rest_framework import status
)from someapp.models import MyModel
)Writing clear and meaningful comments is essential for code maintainability. Follow these guidelines to write effective comments:
Be Clear and Concise: Comments should be easy to understand. Avoid unnecessary verbosity while ensuring clarity.
# Calculates the total price including tax
Explain the ‘Why’, Not the ‘What’: Focus on explaining why a certain approach was taken rather than what the code is doing, which should be clear from the code itself.
# Using binary search for efficiency as the list is sorted
Update Comments When Modifying Code: Always update or remove comments that are no longer relevant after code changes.
Use Docstrings for Functions and Classes: Every function, method, and class should have a docstring explaining its purpose, parameters, and return values.
Example:
def calculate_total(price, tax_rate):
"""
Calculate the total price including tax.
Args:
price (float): The initial price.
tax_rate (float): The tax rate to apply.
Returns:
float: The total price including tax.
"""
return price * (1 + tax_rate)
Avoid Useless Comments: Do not write comments that directly describes what the code does.
# Increment i by 1
# Move to the next item in the list
To maintain a high-quality codebase, adhere to the following best practices:
Write Modular and Reusable Code: Break down large functions or classes into smaller, reusable components.
Adopt a Consistent Naming Convention: Use descriptive names for variables, functions, and classes. Follow the PEP 8 naming conventions:
snake_case
CamelCase
Keep Functions Short and Focused: A function should do one thing and do it well. Aim for functions to be no longer than 20 lines.
Limit the Use of Magic Numbers: Replace magic numbers with named constants for better readability and maintainability.
60
with SECONDS_IN_MINUTE = 60
.Error Handling: Implement robust error handling using try-except
blocks where appropriate. Ensure that exceptions are meaningful and provide clear information about what went wrong.
Write Tests: Ensure that every new feature or bug fix is accompanied by relevant tests. Aim for high test coverage and use descriptive test case names.
Use Version Control Properly: Commit small, logical changes with meaningful commit messages. Follow a branching strategy that suits the project’s workflow.