Whitespace-I’ve Got a Blank Space

Learning Python and the Significance of Whitespace

Sara Khandaker
Nov 20 · 5 min read
Image for post
Image for post

This week was spent learning a new language, Python. It's a fairly easy language to pick up but one thing that's been interesting and new to me is the use of whitespace in Python.

“In computer programming, whitespace is any character or series of characters that represent horizontal or vertical space in typography.”

Up until Python, I’ll admit, I hadn't given whitespace much thought. In general, I thought the compiler didn't care too much about whitespace. Sure, it's necessary to delineate keywords or names of variables, but otherwise, it was optional in the languages I was programming in. The proper use and importance of formating and indenting were stressed as I was learning to code, but only in the scope of making my code more readable for other programmers.

I’m one of those people that like my code to be super smushed together basicallylikethis. And to be honest, I use my IDE to format my code into the proper indentations. I figure I’d let my IDE worry about formatting and indents allowing me more energy to focus on the actual program I’m coding. This worked until the program started to rely on the whitespace for meaning…

Whitespace in Python

Python uses whitespace syntactically. Code blocks are delineated by indentation. Python seeks to be explicit and highly prioritizes readability. This has lead to some very concise and user-friendly syntax. The use of whitespace as a syntactical element is a part of that. Most languages use statement separators, such as semicolons in C#, so multiple statements can be placed in one line. Python uses a newline to separate statements. Additionally, where other languages use tokens such as braces ‘{}’ in Javascript or ‘do end’ in Ruby, Python uses indents instead.

To start a block, place a colon at the end of any line preceding an indent, then you simply indent, and to end a block, you outdent. Luckily a good IDE makes this very easy and there is usually no need to type out the indent.

But still, why use a language that requires me to indent code?

Upsides to Python’s Use of Whitespaces

Readability

The number one benefit of white space is readability. Somewhere in coding history, we realized code isn't written just for computers, it's written for humans too. Whether that's your teammates or your future self. Code has to make sense and be readable to others to allow us to change and maintain it. With Python, they are enforcing good coding styles through the use of indents. Its easier to find a specific block to move or modify because of the whitespace framing. It also reduces inconsistent indentation in code which helps make code more readable.

Adds Meaning

The proper use of whitespace can also add meaning to your code. It creates emphasis and code separated by whitespaces, such as a line before or after the block of code, shows that the block is closer related than its surrounding code.

Clears the Screen

By replacing visible tokens such as “{}” with indents, Python clears the screen. This means you can see more of your code on the screen at a time. This can be appealing since the code looks less cluttered and compact. To learn more about the benefits of seeing as much of your code as possible check out my blog: How Long Should Functions Be? How Do We Measure It? No one likes to see this :

         }
}
}
}

Reduces Redundancy

My favorite reason to enforce whitespace and use it syntactically is that it reduces redundancy. After all, every programming language benefits from indents and we do it anyway! So with that logic, it's redundant to have a language not be whitespace-sensitive. With Python, you type less since you don't need to add the tokens.

Even with all these benefits, it can feel like a lack of flexibility or enforcement of conventions. There are still some parts to this forced use of whitespaces that I need some getting used to…

Downsides to Python’s Use of Whitespaces

Strange Errors

Being dependant on whitespace can lead to some hard to find strange errors sometimes. For example, whitespace might be introduced while doing code merge. In Python, if you skip a line in your code to break it up, that line must have the same indent as the rest of the block. Additionally tracking diffs in whitespace can be very difficult. Even the stuff you can't see in your code can be wrong! This can make debugging a nightmare, but fortunately, most IDEs allow you to ‘show whitespace’ so you can see the invisible.

Inconsistency

One of the first things that came into my mind when dealing with an indent is the big debate over tabs and spaces in general. I won't go too into that but this does show that different people treat whitespace differently and use different amounts and this can lead to inconsistencies. Tabs and spaces can be easy to mix up and even though 4 spaces may look the same as a tab, Python distinguishes the two.

Errors While Moving Code

Since whitespace needs to be correct for code to compile correctly in Python, moving code around can lead to errors. Especially if you are copy-pasting and the white spaces don't copy over exactly. Extra care must be paid when removing a block or adding a block between other blocks since the indentation of the surrounding code must be changed accordingly. If you wanted to comment a block out, it's not quite as easy as commenting curly braces out.

As the number of lines in the code increases, the chances that some indentation has gone wrong increases too.

Conclusion

Wow, I never thought I’d give this much attention to a part of code you can't even see! But I've learned that even empty space is important and is a tool that can be used to make your coding more efficient and readable.

If you are really interested in whitespace, check out this programming language that's comprised entirely of whitespace!

References:

The Startup

Medium's largest active publication, followed by +730K people. Follow to join our community.

Sara Khandaker

Written by

I love seafood, exploring new cities, board games and learning to love to code. https://github.com/sarakhandaker/portfolio

The Startup

Medium's largest active publication, followed by +730K people. Follow to join our community.

Sara Khandaker

Written by

I love seafood, exploring new cities, board games and learning to love to code. https://github.com/sarakhandaker/portfolio

The Startup

Medium's largest active publication, followed by +730K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store