Former-commit-id: 0ca95b2bfa8717926b8044f73b47d000e176b077
pull/160/head
Kye 2 years ago
parent d44a49a6cb
commit 6cc860c41c

@ -0,0 +1,127 @@
# Clean Code
Here are some general principles for writing highly usable, functional, reliable, fast, and scalable code:
1. **Clear and Understandable:** The code should be written in a way that's easy for others to understand. This includes using clear variable and function names, and including comments to explain complex sections of code.
2. **Modular and Reusable:** Code should be broken down into small, modular functions and classes that each perform a single task. This makes the code more understandable, and also allows for code reuse.
3. **Robust Error Handling:** The code should be able to handle all potential errors gracefully, and should never crash unexpectedly. This includes checking for invalid input, catching exceptions, and providing useful error messages.
4. **Type Handling:** Whenever possible, the code should enforce and check types to prevent type-related errors. This can be done through the use of type hints in languages like Python, or through explicit type checks.
5. **Logging:** The code should include extensive logging to make it easier to debug and understand what the code is doing. This includes logging any errors that occur, as well as important events or state changes.
6. **Performance:** The code should be optimized for performance, avoiding unnecessary computation and using efficient algorithms and data structures. This includes profiling the code to identify and optimize performance bottlenecks.
7. **Scalability:** The code should be designed to scale well as the size of the input data or the number of users increases. This includes using scalable algorithms and data structures, and designing the code to work well in a distributed or parallel computing environment if necessary.
8. **Testing:** The code should include comprehensive tests to ensure that it works correctly. This includes unit tests for individual functions and classes, as well as integration tests to ensure that the different parts of the code work well together.
9. **Version Control:** The code should be stored in a version control system like Git, which allows for tracking changes, collaborating with others, and rolling back to a previous state if necessary.
10. **Documentation:** The codebase should be well-documented, both in terms of comments within the code and external documentation that explains how to use and contribute to the code.
11. **Continuous Integration/Continuous Deployment (CI/CD):** Implement CI/CD pipelines for automatic testing and deployment. This ensures that any new changes do not break existing functionality and that the latest version of the application is always available for deployment.
# Examples
1. **Clear and Understandable:** Use meaningful variable and function names. Include comments when necessary.
```python
# Good example
def calculate_average(numbers: List[int]) -> float:
"""Calculate and return the average of a list of numbers."""
total = sum(numbers)
count = len(numbers)
return total / count
```
For file and folder names, use descriptive names that relate to their function in your program. For example, a file that contains functions for handling user input might be named `user_input.py`.
2. **Modular and Reusable:** Write functions for tasks that you perform over and over.
```python
def greet_user(name: str):
"""Print a greeting to the user."""
print(f"Hello, {name}!")
```
For folder structure, group related files in the same directory. For example, all test files could be in a `tests` directory.
3. **Robust Error Handling:** Use try/except blocks to catch and handle errors.
```python
def divide_numbers(numerator: float, denominator: float) -> float:
"""Divide two numbers and handle division by zero."""
try:
return numerator / denominator
except ZeroDivisionError:
print("Error: Division by zero.")
return None
```
4. **Type Handling:** Use type hints to specify the type of function arguments and return values.
```python
def greet_user(name: str) -> None:
"""Greet the user."""
print(f"Hello, {name}!")
```
5. **Logging:** Use the `logging` module to log events.
```python
import logging
logging.basicConfig(level=logging.INFO)
def divide_numbers(numerator: float, denominator: float) -> float:
"""Divide two numbers and log if division by zero occurs."""
try:
return numerator / denominator
except ZeroDivisionError:
logging.error("Attempted division by zero.")
return None
```
6. **Performance:** Use built-in functions and data types for better performance.
```python
# Using a set to check for membership is faster than using a list
numbers_set = set(numbers)
if target in numbers_set:
print(f"{target} is in the set of numbers.")
```
7. **Scalability:** For scalability, an example might involve using a load balancer or dividing tasks among different workers or threads. This is more of a system design consideration than a single piece of code.
8. **Testing:** Write tests for your functions.
```python
def test_calculate_average():
assert calculate_average([1, 2, 3, 4]) == 2.5
```
For tests, you could have a separate `tests` directory. Inside this directory, each test file could be named `test_<filename>.py` where `<filename>` is the name of the file being tested.
9. **Version Control:** This point refers to using tools like Git for version control. A simple example would be committing changes to a repository:
```bash
git add .
git commit -m "Add function to calculate average"
git push
```
10. **Documentation:** Write docstrings for your functions.
```python
def calculate_average(numbers: List[int]) -> float:
"""Calculate and return the average of a list of numbers."""
...
```
Documentation might be kept in a `docs` directory, with separate files for different topics.
11. **Continuous Integration/Continuous Deployment (CI/CD):** This is typically handled by a system like Jenkins, GitHub Actions, or GitLab CI/CD. It involves creating a script or configuration file that tells the CI/CD system how to build, test, and deploy your code. For example, a `.github/workflows/main.yml` file for a GitHub Actions workflow.
Remember, consistency in your naming conventions and organization is key. Having a standard and sticking to it will make your codebase easier to navigate and understand.
Loading…
Cancel
Save