Skip to content

Clean Code

Writing clean, readable, and maintainable code is crucial for reproducible research. This section covers Python coding standards, linting, and formatting tools to help you write high-quality, consistent code.

PEP 8: Style Guide for Python Code

PEP 8 is the official style guide for Python code. Following PEP 8 ensures consistency across Python projects. Key points include:

  1. Indentation: Use 4 spaces per indentation level.
  2. Maximum Line Length: Limit lines to 79 characters for code, 72 for comments and docstrings.
  3. Imports: Place imports at the top of the file, grouped in the following order:
    • Standard library imports
    • Related third-party imports
    • Local application/library specific imports
  4. Whitespace: Use blank lines to separate functions and classes, and larger blocks of code inside functions.
  5. Naming Conventions:
    • Functions, variables, and attributes: lowercase_with_underscores
    • Classes: CapitalizedWords
    • Constants: ALL_CAPS
  6. Comments: Use inline comments sparingly. Write docstrings for all public modules, functions, classes, and methods.

Linting

Linting helps identify programming errors, bugs, stylistic errors, and suspicious constructs. Popular Python linters include:

  1. Pylint: A comprehensive linter that checks for errors, enforces a coding standard, and looks for code smells. Installation: pip install pylint Usage: pylint your_script.py

  2. Flake8: Combines PyFlakes, pycodestyle, and McCabe complexity checker.

Installation: pip install flake8 Usage: flake8 your_script.py

  1. Mypy: A static type checker for Python.

Installation: pip install mypy Usage: mypy your_script.py

Formatting

Automatic formatters help maintain consistent code style across your project:

  1. Black: An opinionated formatter that adheres to PEP 8 guidelines.

Installation: pip install black Usage: black your_script.py

  1. YAPF (Yet Another Python Formatter): A formatter by Google that's highly configurable.

Installation: pip install yapf Usage: yapf -i your_script.py

  1. isort: A utility to sort imports alphabetically and automatically separate them into sections.

Installation: pip install isort Usage: isort your_script.py

Additional Best Practices

  1. DRY (Don't Repeat Yourself): Avoid duplicating code. Extract repeated logic into functions.

  2. KISS (Keep It Simple, Stupid): Write simple, straightforward code. Avoid over-engineering.

  3. YAGNI (You Aren't Gonna Need It): Don't add functionality until it's necessary.

  4. Write Self-Documenting Code: Choose descriptive variable and function names that explain their purpose.

  5. Use Type Hints: Employ Python's type hinting system to improve code clarity and catch type-related errors early.

Example: python def greet(name: str) -> str: return f"Hello, {name}!"

  1. Error Handling: Use try/except blocks to handle exceptions gracefully.

  2. Testing: Write unit tests for your functions and classes using frameworks like pytest.

Integrating with Your Workflow

  1. Pre-commit Hooks: Use pre-commit to run linters and formatters automatically before each commit.

  2. CI/CD Integration: Include linting and formatting checks in your Continuous Integration pipeline.

  3. IDE Integration: Configure your IDE (like PyCharm or VS Code) to run linters and formatters on save.

By following these guidelines and using these tools, you'll produce cleaner, more maintainable, and more reproducible code. Remember, consistency is key in collaborative projects, so agree on a style guide with your team and stick to it throughout your research project.