Designing software architecture is one of my favorite parts of my job. It’s extremely gratifying to build something others can work with and extend upon. Creating good architecture involves writing code that is clean, clear, and hits the mark for the given problem space. The same is true for writing good code in general.
Identifying increasingly elegant and appropriate solutions is a skill that developers hone over their entire careers. The more you’ve cultivated that skill, the easier it is to produce targeted, understandable code to address the problem at hand. Unfortunately, this is a process bound by time. However, writing clean code is something everyone can begin immediately simply by leveraging free tools like Pylint, djLint, and Black.
What does clean code look like?
When code is clean, it’s formatted consistently. It doesn’t have unnecessary imports. It adheres to the team’s coding standards. It looks well-organized and visually appealing.
It’s easy to contribute clean code to projects by leveraging tools like linters and formatters. In my preferred editor, VS Code, you can install these tools as extensions. That provides immediate feedback, thereby improving code quality and developer experience.
Tip! Save your configurations and VS Code workspace config in your repo. That way, everyone contributing to the codebase can use the same tools and configurations.
Pylint is a static code analyser for Python. It can look at your code and warn you about potential errors before your code runs. Some of the things it can do include:
- checking code for adherence to a coding standard
- detecting unused imports
- warning about line length
If installed via VS Code, it will automatically run on open files and will highlight lines of code you need to fix.
The default configuration of Pylint is pretty solid. It enforces many rules that, if adhered to, will leave your code in a good state. If you need customization, you can do that by adding a
.pylintrc file to your root directory.
Since the next thing we’ll do is set up the code formatter called Black, I recommend you DO create a
.pylintrc file and add the following:
[FORMAT] max-line-length = 88
Black is a code formatter specifically for Python. Code formatters restructure the code in your files so that spacing, indentation, and line length are consistent. This is powerful when applied on save because it makes the functionality part of the developer’s normal workflow.
There is no configuration for Black, and that is by design. It enforces its own particular style so that developers can focus on writing code, not configurations. If that’s too rigid, there’s a similar project called Blue that offers more flexibility.
Before you can use Black, you’ll need to install it with
djlint is both a linter like Pylint and a code formatter like Black. Instead of using it for Python files, it’s used specifically for jinja2 templates. This is helpful when working in a Flask application’s HTML templates, which can sometimes grow quite large. The tool is great because it can:
- properly format jinja2 syntax within the HTML document
- detect improperly formed tags
- quickly format large HTML trees
Like Black, you must install djLint with
Improve Code Quality in Python Flask with Pylint, djLint, and Black
With these tools, you can focus more on writing clear elegant code and less on cleanliness.