avatarMarcin Kozak

Summary

The article discusses the significance of Python's indentation rules, arguing that they contribute to cleaner and more hygienic code despite criticism and comparisons to languages with more flexible indentation practices.

Abstract

Python's strict indentation rules are often criticized for potentially making the language's syntax difficult and error-prone. However, the article defends these rules, suggesting that they enforce a consistent and clean code style, which is preferable to the freedom of using curly brackets as seen in languages like Java, C#, or R. The author, with a background in R, shares their transition to Python's indentation style, emphasizing the benefits of a uniform syntax for code clarity and readability. The article presents examples of Python code with and without strict indentation, concluding that Python's approach to indentation is a strength, not a weakness, and contributes to the language's simplicity and organized structure. The author encourages readers to reconsider the common aversion to Python's indentation and to appreciate its role in maintaining code hygiene.

Opinions

  • Critics argue that Python's strict indentation rules can lead to errors and are a drawback compared to the freedom in languages like Java, C#, or R.
  • The author believes that Python's indentation rules create a clean and clear code structure, referring to it as "syntax hygiene."
  • The article suggests that the use of curly brackets to replace indentation could lead to "indentation anarchy," resulting in code that is less clear and more difficult to read.
  • The author values the consistency enforced by Python's indentation, considering it an advantage and a key factor in Python's code simplicity.
  • The article downplays the difficulty of copying code with indentation and the issue of mixing tabs and spaces, proposing style guides and tools like black to maintain consistency in team environments.
  • The author advocates for Python's indentation style as a great strength, making Python code more organized and hygienic compared to the messier alternatives in other languages.

Python Indentation: Is It That Bad?

Does it make Python’s syntax difficult and prone to errors?

With or without indentation, as in visual order or visual clutter? Photo by Rick Mason on Unsplash

So many authors criticizing Python, even here on Medium, point out that one of the biggest problems of Python is its strict indentation rules. Let’s consider if this is indeed the case.

Python does require you to implement a particular indentation style, and it’s very strict about that. If you don’t, you risk getting IndentationError or, even worse, incorrect code. Critics like to list other languages, like Java, C#, or R, which let you indent your code however you want, and they stress how much they miss this freedom in Python.

Would indentation freedom lead to better Python code? Is indentation really that bad a thing? Do you really miss that indentation freedom? To answer these questions, let’s jump into some examples.

Examples

Consider the following code:

It’s code for a mockup of an app rather than of an actual app, but this does not matter. I do not ask you to read the code in detail. Instead, just look at it and pay attention to the use of indentation and curly brackets. Pay attention to whether you like the style of the syntax.

Yes, it’s Python! But it’s a different Python; a Python from a different world; a Python that gives you indentation freedom — indent the way you want, and it’s fine. But of course, we cannot simply give up indentation. We need something else to organize the code. So, the code above uses curly brackets. This solution seems like a good idea. These other languages that do not use indentation use curly brackets, and hence our please-remove-indentation-from-Python Python uses them, too.

This does not mean, of course, that you must not use indentation in our new Python. Do whatever you want! You want to use the regular Python indentation style? Do it. You want to break its flow? Do it. Do whatever you want, but in so doing, do not forget to use curly brackets. Indentation freedom, here we come!

What a relief that we can use the function dict() instead of a dictionary literal, {}, or a dictionary/set comprehension! I am not sure that it would be easy to parse Python code that uses curly brackets for dictionaries and sets and as an indentation replacer. No problem for us, fortunately. Let others worry about that if anyone decides to actually implement this new Python.

Since we’re free, we can use a different style. Here’s what it looks like:

And this is regular Python in action:

This last code works, you can check it. Of course, the app does not fit a real model, just some artificial one.

What do you think? Do you like the freedom that the curly brackets Python offers? Or do you prefer the good ol’ Python with its indentation?

My Indentation Story

When I came to Python, I had been using R for about 16 years. Sixteen years! Sixteen years of freedom and curly brackets! I was free. No one made me use spaces or tabs, and I could use tabs and spaces and whatever I wanted — or even nothing, just curly brackets.

So, how come I decided to replace this freedom with Python’s strict rules?

There were many reasons, but an important one among them was what I called Python’s syntax hygiene. This was, to be true, one of the first things that I thought about Python’s syntax, and, for that matter, indentation. You must use indentation in Python, and it makes code clean! Clean and clear.

Yes, exactly. After sixteen years of curly brackets and so many various styles of using them, I considered Python’s indentation hygienic. This means that the syntax is clean; the code is clear and easy to read; and the additional whitespace is refreshing, making the code visually less cluttered and thus cleaner.

You don’t have to agree with me, but when I look at the three code snippets above, I definitely consider regular Python to be the best. It’s hygienic. It’s clear. And the other two snippets, written in please-remove-indentation-from-Python Python, get on my nerves. I do not call this indentation freedom; I call it indentation anarchy. Do whatever you want, and it will be fine as long as you just remember to correctly use curly brackets. No, this does not work for me. I don’t want to see indentation anarchy in Python code; I want to see hygiene in Python code! Please, keep Python hygienic!

Conclusion

What’s wrong with indentation? I’ve heard it’s bad because copying code can sometimes be difficult. Really? For the last four years, I have not once thought it was really a problem when I copy-pasted code. The infamous IndentationError? Ah, yes, I remember, I sometimes put a space before code in an interactive session, enough to remove the space and all’s fine.

I’ve also heard indentation is bad because a developer can mix tabs and spaces. But why would anyone mix them? Teamwork can lead to such issues, though. But no matter which language you work with in a team, you should create a style guide, and all should follow it. This alone should be enough to solve this particular indentation problem. Or, you can simply use black or a similar tool.

Frankly, I don’t understand this whole aversion to indentation. For me, it’s a great thing. It makes Python code so hygienic, so clean, so clear. The additional whitespace makes code a little less cluttered and better organized. And I do remember the mess I had to struggle with when I used R’s curly brackets. No hygiene, no order, just a mess unless a team chooses a particular style and follows it, but then all those curly brackets closing if-blocks, loops, functions, and so on… Thank you very much! I prefer four spaces.

Let me repeat that I understand and accept that you can have a different opinion. You choose to hate Python’s indentation, fine by me. But please, do not say that Python is not the best programming language because of indentation:

“A point to note is that indentation is not optional in Python. This poses a problem when using conditions. Also, indentation makes it hard to see where the function ends.”

Really?

I think the time has come to give indentation some credit. I think the simplicity of Python code comes from various reasons, and one of them is nothing more than indentation itself.

Thanks for spending this time with me. I will be happy to learn what you think about this surprisingly controversial topic, so please share your thoughts in the comments.

Do you think Python’s indentation style is advantageous or disadvantageous?

Do you think Python would do better without it, using curly brackets instead? Or, maybe, you have a better idea for solving the Python indentation issue?

Perhaps, do you agree with me that indentation is Python’s advantage, its great strength, and what makes its code hygienic?

Resources

Programming
Python
Indentation
Coding
Clean Code
Recommended from ReadMedium