Today we are going to look at two ways to make your Python code cleaner.
What does that mean? Well, code is very often read by people other than the person who wrote it, for purposes of maintenance, integration into larger systems, debugging etc. At school, it could be your teacher who reads it. It’s also worth remembering that you yourself have to read your code as you develop it, and that if you come back to your own code after even a short time, you might as well be another person in terms of understanding it (If you doubt this, maybe try a little experiment…).
In Python there is a set of guidelines for writing code which is eaay to read and work with, regardless of who wrote it. This set of guidelines is called PEP 8. It contains advice on things like:
- Naming conventions
- Code Layout
- Line length
- Indentation
- Use of comments
- General approaches based on simplicity
One of the core values which these guidelines are designed to support is Readability.
It is a good idea to begin to incorporate some of the guidelines from PEP 8 as early as possible in your learning journey so as to establish good habits.
Online PEP 8 Checker
One way to do this is to get into the habit of checking your code against the standards using some kind of tool. There are many ways to do this, but for short programs, one of my favourite ways is to use the PEP 8 online checker.
Simply paste your code and click the button to see a list of all the violations of PEP 8 standards you are guilty of. You may be surprised at how “fussy” the suggestions are, but doing what is needed to keep the checker happy is a great way to develop the discipline you need to write better code.
PyCharm Reformat Code
.
In a previous article I discussed various editors you can use for programming with Python. One of the was PyCharm community edition.
Personally, for small projects, I prefer to use a more basic text editor then PyCharm, like Notepad++. This is because I don’t always want to create a whole project structure for a simple program and also it takes a while to start up. However, when I am near to completing a program, I often open up PyCharm to make use of just one incredibly useful feature – the Reformat Code
option under the Code
menu. One click, and all your little programming peccadilloes will vanish into thin air. Things like spacing issues, bad use of blank lines, indentation problems etc. all gone. You can configure many options for the reformatting, but I find the default options generally suffice.
Some might argue that you should endeavour to avoid making these errors in the first place and I agree. However they still happen, at least to me.
Using the PyCharm Reformat Code
option in combination with the PEP 8 online checker mentioned above is a great way to make sure your code avoids some of the more common errors which programmers make in terms of writing clean code. To me it makes sense reformat you code before using the PEP 8 checker. For large projects this approach not really viable, but for small programs, it provides a great way to make sure you are turning out reasonably clean code.
Of course clean code involves much more than these basic issue, but this is great start. You can read more about clean code here at simpleprogrammer.com.
A Foolish Consistency is the Hobgoblin of Little Minds
Beware however, that slavish dedication to absolute rules is rarely a good idea. The python.org site mentioned above reminds us of this:
However, know when to be inconsistent — sometimes style guide recommendations just aren’t applicable. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don’t hesitate to ask!