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:
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.
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.
DRY (Don’t Repeat Yourself)
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.
KISS (Keep it simple stupid)
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
What do we expect?
- Maintain simple code out of duplication.
- Make it self-descriptive.
- Quality Code.
What can we do?
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.
SOLID Principles — explained with examples
Writing a code which satisfies present requirements, also which can satisfy future requirement easily, should be the…