Formatting Your Python Code for Optimal Readability
The skills we demoed here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.
In a recent article, you learned to create clearer and more concise documentation of your Python code. Additionally, you gained a better understanding of the importance of the process for better collaboration. In this article, we will show you a few ways to further improve your code’s readability by learning about some styling best practices to make your code more beautiful and interpretable.
Coding beautifully is one of the best ways to make your code more interpretable. But what does beautiful code look like?
- The syntax is separated into logical sections.
- There aren't too many characters on a single line
- Indentation is correct (this is necessary for Python)
- There is correct and consistent spacing between arguments in lists, dictionaries, functions, and between operators and operands.
All of this can help the code reader analyze your syntax logically, which will help them understand the function of the code and how to use it.
Although technically there is no absolute “right way” to do this, some are better than others. The most important thing to remember is to be consistent with whatever style you choose.
There are a few styles you can use to make your code look great. As long as the style you choose is an acceptable convention, you can use what works best for you. Never swap between different styles throughout your code. This makes it confusing and hard to follow. Once again, consistency is key!
Code that has few spaces or no lines separating code chunks can be hard to read and, therefore, hard to follow. It’s good practice to add empty lines in between your code. Being aware of how your code looks on the screen is extremely important, though this can be difficult to recognize when you’re the one writing the code.
Having someone else look at your code is a great way to see if it seems too cluttered. We suggest you put spaces between logical segments to separate your thoughts within your code.
You don’t want to put spaces between function names and the parentheses containing the arguments. This syntax will technically work, but it doesn’t look well put-together. In the example above, everything is crowded and hard to read through. Inconsistencies in spacing can be a problem in the future when you come back to use your code.
A good rule of thumb to follow is even spacing within your code, like in the above screenshot. You should put a space before and after operators. Another technique to mind is using lines between chunks of code to delineate your thoughts.
Max Characters On a Line
PEP8 suggests no more than 79 characters on a single line. However, counting characters on each line can get tedious and become a waste of time. Think of this instead: does your statement sound like a run-on sentence? If it does, split it up into separate lines.
A few justifications have been given for this seemingly random number of 79. There are psychological implications in terms of reading comprehension based on line length, where after about 75 characters, people cannot absorb information as well. Many devices can also show only around 80 characters on a single line (with a standard font). Using this convention for maximum line length keeps statements on a single line on the screen, even when people set up windows side by side on a monitor.
When you split up a statement that contains operators onto multiple lines, make sure the operator precedes the operand.
When you split up a function call and it's arguments onto multiple lines, ensure that the indentation is aligned.
You will often write functions that involve multiple operations. Trying to understand the inner workings of a function that contains a lot of raw code can be overwhelming.
A helper function is used to take a portion of code from a bigger function and move it elsewhere. This is done to segment code into different parts for improved readability and to compartmentalize your thoughts.
Helper functions contain smaller portions of the overall goal. The main function then calls these smaller functions. This way, you can more logically segment the steps you take to get your finished product.
This information is merely scratching the surface of the broader topic of Python Code Readability. Making your code beautiful is a great way to allow others to fully understand all you’ve written and impress those you’re working with. Good looking, functional code is a topic extensively taught and stressed in our bootcamps and programs.
Ready to advance your programming skills to the next level? Check out this three-course program focused on building and advancing your Python Programming skills, or start your journey towards data science mastery by enrolling in our upcoming remote live and online data science bootcamps this Winter!