Pylint: Code Cleanliness for your Python Projects

https://www.milanlatinovic.com/clean-code-rules/

This article was written as an individual review assignment of PPL CSUI 2021

Writing code is always easy. Writing clean code, however, is a challenge that many programmers sometimes ignore. It is not an exaggeration if someone were to say that writing clean code is like common sense for us programmers because it really is. But it is also not an exaggeration if someone were to say that writing clean code is tedious. Because it really is.

Python, like many other programming languages, has tools that may help its programmers to write cleaner code. The tools are often called linters. One linter, in particular, that is called Pylint is one of the most popular ones. This article aims to explain what clean code is in Python and how Pylint may help you write cleaner code.

What is clean code anyway?

A clean code is a code that follows a certain set of rules, values, and principles that set the bar on how good the code should look and operate. A clean code can be easily read, understood, and added/changed. We strive for code cleanliness because most of the time, we not only write code for a machine to understand but also for other people to understand, which means that if we write dirty code, our fellow programmers might be going through a nightmare when adding, changing, or fixing our code.

What is Pylint?

Pylint is a tool that can help Python programmers to detect dirty codes. Pylint is a popular Python linter because of its helpful features that include, but not limited to:

  1. Coding standard
  • Checks our program’s line-code’s length, see whether or not it is too long
  • Checks if our program’s variable names are well-formed according to our coding standard
  • Checks if imported modules are used
  • Checks for duplicated codes in our program
  • and more.

2. Error detection

  • Checks if the declared interfaces in our program are truly implemented
  • Check if required modules are imported
  • and more.

And another reason why many Python programmers like Pylint is because it is fully customizable. We can re-configure the default rules that Pylint has and we can write a small plugin to add a personal feature.

In addition to that, we can also integrate Pylint in our project’s continuous integration (CI) which may be useful if you need it.

How do I use Pylint?

Here is a simple guide for installing and using Pylint in your Python projects.

Installing Pylint

You can install Pylint by using this pip install command:

pip install pylint

Running Pylint on a Python program

You can run Pylint on a program using a command like this:

pylint your_program_here.py

After running that command, you will be shown whether or not the program is clean, and Pylint will score your code cleanliness as well.

For example, here is a case where Pylint thinks my code is already clean:

As you can see, Pylint gives me a score of 10/10 for the cleanliness of my program which is called formulas.py.

Here is a case where Pylint thinks a program is not clean enough:

As you can see, Pylint gives me a score of 9.09/10 for the cleanliness of a program called manage.py and gives detail to what makes it dirty.

Running Pylint on a Django project

To run Pylint on a Django project, you will need an additional requirement that is called pylint-django, you can install it like this:

pip install pylint-django

To run Pylint on your Django project, you can use a command like this:

pylint --load-plugins pylint_django <insert your django apps here>

For example, in my PPL group project, we have apps called dietela_backend, dietela_program, dietela_quiz, nutritionists, authentication, and payment , so we run a command like this:

pylint --load-plugins pylint_django dietela_backend dietela_program dietela_quiz nutritionists authentication 
payment

And after that, Pylint will show your Django project’s code cleanliness like so:

Examples of how Pylint helps me in my PPL group project

Here are some examples of dirty codes that were in my PPL course group project and how Pylint helped me to detect them:

Snake case variable naming style

If you’re a Python programmer, then you must know the variable naming style convention of Python, which is the snake case naming style.

Pylint noticed that a piece of code in my project had a camel case naming style, so when I ran Pylint on my project, it informed me to change it to a snake case naming style.

Absurdly lengthy lines of codes

When a line of code gets too long, it really can annoy anyone who’s reading it.

Pylint noticed that a piece of code in my project had absurdly lengthy lines of code, so when I ran Pylint on my project, it informed me to cut it to multiple pieces.

Messy imports

Most of the time, we import a lot of things when we write our code. When there are too many imports in one place, they may get a bit confusing and hard to read if they are not properly placed.

Pylint noticed that there were imports in my program that were not ordered well and not grouped together, so when I ran Pylint on my project, it informed me to reorganize my imports.

These are only a couple of examples, but Pylint has helped my team a lot to make sure that we only write clean code and so, I hope that Pylint may help you as well.

That is all from me, friends! :)

God bless.

References

--

--