Clean Code in Python

Esteban Solorzano
Jun 16, 2020 · 4 min read

“A long descriptive name is better than a short enigmatic name. A long descriptive name is better than a long descriptive comment.” Robert C. Martin

When writing code we are constantly making changes, adding functionality that we require or just removing the one we don’t need anymore. Learning a programming language and knowing how the algorithms work can be difficult but writing clean code is often even more complicated.

Clean code is a set of principles that seeks code to be:

  • Readable
  • Maintainable
  • Extendable

It is completely important to adopt some good principles when writing code since we usually have the bad habit of naming variables like a or b… What do they mean? You’ll probably remember them while you’re working, but what if you come back in a couple of days, will you remember them?

The philosophy of Python aims to make the code “Pythonic”. One of its developers, Tim Peters wrote the Python Zen which is a set of principles of how we should write code.

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one — and preferably only one — obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

I like the intention of this Zen and especially the principles:

  • Beautiful is better than ugly
  • Simple is better than complex

We should think simple, if a functionality does not require much effort, then why do we do it? not always something simple is the solution, but if it can be done, do not make it complicated.

PEP8 is a proposal for a style guide for Python code, it is optional, however it makes it easier to write better code, improving readability and quality.

This principle promotes that the code we create should be unique, avoiding or reducing duplicated code. The idea of this principle is to be able to divide the code into small pieces we can reuse.

Adding unnecessary code increases the technical debt, difficulty in making changes, and might generate inconsistencies in the project.

The KISS principle intends to make code development as simple as possible. It may sound easy to do but it isn’t and this can be caused because we don’t know how to write good code.

Any fool can write code that a computer can understand. Good programmers write code that humans can understand. — Martin Fowler

  • Maintain simple code out of duplication.
  • Make it self-descriptive.
  • Quality Code.

To write Pythonic code, we must know the features provided by the proper language, functionalities that allow us to easily understand and improve the readability as well. Dunder methods, Context Managers, and Decorators are some of the features we can use to achieve this.

Decorators can help us to avoid duplication and separate logic in small reusable pieces.

Context managers help us to work with those functions that require conditions before and after completing a task, for example reading or writing files, opening them before processing them, and closing them once we no longer need them.

Dunder Methods or Magic Methods help us to write better our code, enriching the classes so that the code remains simple and understandable but with higher functionality.

At the end of the day we want to write code that makes sense, so it won’t blow up in our faces and we will have to go back to it to solve what we could have prevented in the first place.

Remember that writing clean code is not for us, it is for our future self and for those with whom we work.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Esteban Solorzano

Written by

Software developer with passion for technology and development.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Esteban Solorzano

Written by

Software developer with passion for technology and development.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store