Whitespaces Can Reveal Your Coding Skills and Determine Your Salary (Maybe!)
By: Naser Tamimi
Tags: Technology, Data Science, Programming, Python, Computer Science

PROGRAMMING

Whitespaces Can Reveal Your Coding Skills and Determine Your Salary (Maybe!)

How to use whitespaces properly to showcase your coding skills

Photo by iMattSmart on Unsplash

In the world of programming and data science, most people don’t know you personally, and they judge you based on your codes. The reality is they look at your code performance as well as your code appearance. The appearance of your code (i.e., style) can reveal too much about your coding skills. For people like me whose background is not computer sciences but their everyday job is to write codes, the most important question after learning “how to code” is how makes your codes more professional. After all, we are writing codes to be read by machines as well as humans, and we don’t want to look like amateurs (although we know we are). Through my jobs and interacting with professional developers, I learned a lot in this space. But one of the trickiest parts of coding style is how and when we should use whitespace. There are some written and unwritten rules that your code would be read better if you followed, and people will appreciate your coding skills.

Whitespaces

In programming, we generally use two types of whitespaces: horizontal and vertical. In Python, horizontal whitespaces and indentation are an essential part of the language. We must use indentation to tell Python how to run the code. In many languages, indentations and horizontal spaces are optional (still highly recommended) since they don’t rely on whitespaces to compile or interpret the code.

Besides indentation, other types of whitespaces are optional. But as you will see, there are recommended rules on using whitespaces that make reading the code easier for humans.

Note: Python has a good and easy-to-follow guide on code style called PEP8. If you are not familiar with PEP8, I highly recommend looking at it after reading this article. Some examples are rules in this article are borrowed from PEP8 documentation. Here is a link to know more about PEP8.

Horizontal Whitespaces

As the name suggests, horizontal whitespaces are spaces that we use inside a line of code. Fortunately, the styling rules for this class of whitespaces is more defined and discussed among developers. We take a look at some of the most important ones.

Indent your code blocks with a tab or 4 spaces.

This is a holy war among professional developers to use spaces or tab for indentation. Apparently, it can even affect your salary (read here) and can lead to breaking up with your girlfriend (watch here). If you don’t have a preference, pick one consistent style. Google advises its Python developers to use 4 spaces for a code block (link). A final thought on this topic, it does not matter if you use tabs or spaces as long as you don’t mix them and you are consistent.

Expression and statements

You use expressions and statements regularly in a code. There are some special rules on how to use whitespaces to style them. Here is a table of the most important ones.

Vertical Whitespaces

Vertical whitespaces are blank lines between lines of a code. As said before, rules related to using horizontal whitespaces are well defined. On the contrary, the rules for using vertical spaces are not clear. Using vertical spaces is a more personal preference. Here are some tips for using vertical spaces that I think most professional developers agree on them.

End of your code

Remember to leave a blank line at the end of your code. Just check to have no spaces in your last blank line. It is probably the most famous blank line you will find among professional developers who follow PEP8.

Functions, Classes, and Methods

Classes and top-level functions should be separated by two blank lines. Also, methods inside a class should be separated by one blank line (link). Try to avoid using too many blank lines inside a function or method unless your function is too long and it does different functionalities (which is a warning sign).

Docstrings and long comments

It is a good practice to leave comments in your code. Many professional developers have at least one docstring (multi-line comment) at the beginning of their code (copyrights, licensing, author, and a brief introduction to the code). Some other developers use long comments and docstrings inside the code to clarify their functions or classes. If you follow this good practice, it is important to use 1 (or 2) blank line before and after your docstrings and long comments.

Conditional statements and context managers

Although it is advised to use blank spaces sparingly, I found it professional to use one blank space around long IF(or any conditional) statements as well as WITH statements (or any context manager).

Don’t Memorize and Use Linters.

I mentioned the most important rules for using whitespaces. There are many other guidelines (e.g., hanging indentation) that I did not mention in this article. But don’t worry, you do not need to memorize all of them at the beginning. Instead, I recommend you to use linters. Linter or lint is a tool that checks the code against provided guidelines like PEP8. Python programmers usually use either pylint or flake8 for checking their code style and many other static mistakes. Let’s install and test both tools quickly.

pip install pylint flake8

Now, it’s time to write bad code (I am good at it!!!)

There are several issues with this bad code. Let’s run pylint and flake8 to see if those tools can find the mistakes.

pylint whitespaces_bad_practice.py

Here is the result.

pylint checks many guidelines in addition to whitespaces. In this report, it has three issues related to whitespaces. As you see, two issues are regarding using 2 whitespaces instead of 4 whitespaces for indentation. The other issue is, I forgot to leave a blank line at the end of the code.

Let’s test flake8 …

flake8 whitespaces_bad_practice.py

And, here is the result.

As you see, flake8 could find more mistakes, including whitespaces before brackets and colons. However, none of them could catch mistakes like x= 1 or x==1 .

Summary

Using whitespaces (horizontal and vertical) makes your code easier to read and understand. My personal experience is that beginners tend to use too much spacing or not enough spacing, and professional developers note that. Remember, you must use vertical spaces sparingly, but if it helps your code to be understood better, use them. Also, check with your company to see if they have a specific Python style guideline for their developers. If they don’t, you can follow PEP8 general guidelines (link), use tools like pylint or flake8 or follow guidelines from companies like Google (link).

Let me know if you have any experience with whitespace and how to use them properly.

Gain Access to Expert View — Subscribe to DDI Intel