Tips and Tricks for Clean and Pythonic Code | Part 1

Code Formatting

Welcome to this series Tips and Tricks for Clean and Pythonic Code, where I’ll take you in a short journey to the world of clean and pythonic code, based on my humble experience with Python and my readings in these topics.

In the first part, I’ll talk about Code Formatting and Black Formatter.

The mentioned tips and tricks are applicable to other programming languages, but some mentioned ideas are a Python-related ones. What we are waiting? Let’s begin…

Code Formatting

Tim Peters says in The Zen of Python aphorisms (the 1st one):

Beautiful is better than ugly.

And in the 7th aphorism, he said:

Readability counts.

Although the code formatting doesn’t affect the code execution process of the Python code on your computer, but it affects the human readability. In other words, your code will be hard to be maintainable by others and even YOU!

Readability is one of the major reasons for Python popularity between professional developers and newbies too.

Following style guides (a document that outlines a set of formatting rules for software developers to follow) is a popular way to make your code a well-formatted code, and there are many style guides for programming languages and Python as well.

PEP 8 — Python Enhancement Proposal 8, is one of the popular style guides for Python, where its created by the Python core development team (Guido and others). It’s worth mentioning that you’ll see some large companies and software teams are forming their own style guide too.

Now, I’ll walk through some do and don’t do practices for more clean and pythonic code.

Don’t:

dont1.png

Don’t put spaces before separators and function’s parameters.

Do

do1.png

Horizontal spaces aren’t the only spaces you should take care about, vertical spaces are affecting code readability too.

Don’t

dont2.png

Do

do2.png

Add a new line between functions, methods, conditions and loops if they are in the same scope. And add two new lines between classes and functions if the scope is different.

As you noticed above, there are a lot of details in these types of best practices while coding, so what are the tools that can help here?

Black Formatter

Black formatter will be a good friend for you while coding to follow PEP 8 style guide. But what is Black?

Black is a PEP 8 compliant opinionated formatter. Black reformats entire files in place. To get started with Black, use this command to download it on your Python environment:

pip install black

To use it with your Python files, use this command:

python -m black {file_name.py}

Notice that it doesn’t take previous formatting into account, it’ll reformat it completely.

You can see and test some examples with Black formatter on the Playground online editor from here.

Recap

One of the most important code readability factors in coding is code formatting.

There are many style guides to follow while coding, PEP 8 is the popular one from Python core development team.

Black formatter is a software that takes care about your code to ensure that you’re following PEP 8 style guides while coding in Python.