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!Β
Spacing
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.
Helper Functions
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!