Python Morsels: Embrace whitespace
pythonmorsels.com·2d
✍️Markdown
Preview
Report Post

Your code is easiest to read just after you’ve written it. Your future self will find your code far less readable days, weeks, or months after you’ve written it.

When it comes to code readability, whitespace is your friend.

Whitespace around operators

Compare this:

result = a**2+b**2+c**2

To this:

result = a**2 + b**2 + c**2

I find that second one more readable because the operations we’re performing are more obvious (as is the order of operations).

Too much whitespace can hurt readability though:

result = a ** 2 + b ** 2 + c ** 2

This seems like a step backward because we’ve lost those three groups we had before.

With both typography and visual design, more whitespace isn’t always better.

Auto-formatters: both her…

Similar Posts

Loading similar posts...