Code quality refers to the overall quality of the code. It takes into consideration things like encompassing readability, maintainability, efficiency, and adherence to best practices.
- Code management (reducing bugs and errors)
- Facilate collaboration with other team members
- Easing maintenance
- Easing future updates
- Improve reliability
- Improve software and team performance
- Technical debt
- Increased development costs
- Decreased productivity over time
- They are agreed by a team or community
- They chosen because a group/community agrres that they lead to more readable and maintainable code.
- Consistent naming conventions
- Proper code organization and modularization
- Effective use of comments and documentation
- Following the Don't Repeat Yourself (DRY) principle
- Writing testable code
PEPs are design documents that provide information to the Python community or describe new features for Python or its processes. PEPs serve as the announcement tools for propose new features, talk to the python, and document decisions.
Okay, but what does that has to do with quality code?
PEP 8 provides coding conventions for the Python code comprising the standard library in the main Python distribution. It is one of the most popular coding standards and many projects follow PEP 8 to maintain consistency across the Python ecosystem.
- Indentation: Use 4 spaces per indentation level.
- Maximum line length: Limit lines to 79 characters for code, 72 for comments and docstrings.
Naming conventions:
- Functions, variables, and attributes: lowercase_with_underscores
- Classes: CapitalizedWords
- Constants: ALL_CAPS_WITH_UNDERSCORES
Coding Structure
- Imports: Should be on separate lines and grouped (standard library, third-party, local)
- Whitespace: Use blank lines to separate functions and classes, and larger blocks of code inside functions
- Comments: Use inline comments sparingly, write docstrings for all public modules, functions, classes, and methods
A linter is a tool that analyzes source code. It is kind of a grammarly for code. It detects programming errors, bugs, and stylistic errors.
Linters do not execute the code.
Linters find:
- Syntax errors
- Unused variables or imports
- Violations of coding standards
- Potential logical errors or bugs
- Security vulnerabilities
Why use linters?
- Early detection of errors
- Consitency
- Time saver
- Code quality improvement
- Integration with CI/CD
- Help developers learn best practices and improve their coding skills
pip install pylint
- Basic usage:
pylint your_file.py
- Installation:
pip install black
- Basic usage:
black your_file.py
- Installation:
pip install ruff
- Basic usage:
ruff check your_file.py
- Pylint: Comprehensive linter, checks style and logic
- Black: Opinionated code formatter, focuses on consistent style
- Ruff: Fast linter, combines multiple tools, customizable
- I always use Black and Ruff, but I only use pylint once the code is more "mature".