Post

Clarity Over Compactness — Writing Python That Speaks

When I started learning Python, I had this urge to write code that felt clever—tight, minimal, efficient-looking. You know, code that felt like a wink to other programmers. But over time, I realized that cleverness often came at the cost of clarity.

Python’s philosophy nudges us in a different direction. You can literally import this and see one of the most important lines:

“Explicit is better than implicit.” “Readability counts.”

That doesn’t mean our code should be verbose or bloated, but it does mean that we should choose clarity over compactness—especially when those two are in conflict.

Let’s walk through some real-world-ish examples where the clearer approach wins.


1. List Comprehensions vs. Readability

List comprehensions are great. But just because you can nest three of them into a single line doesn’t mean you should.

1
2
3
4
5
6
7
8
9
# Compact, but dense
result = [x*y for x in range(5) for y in range(5) if x != y]

# Clearer
result = []
for x in range(5):
    for y in range(5):
        if x != y:
            result.append(x * y)

That first version? It’s fine if you’re doing something simple. But if someone reading your code has to pause and untangle it in their head—it’s not helping anyone, including your future self.

List comprehensions are slightly faster, yes—but performance rarely matters for small loops. Readability almost always matters.


2. Ternary Operators Are Not Always the Answer

Ternary operators (x if condition else y) are elegant and useful, but readability drops fast when logic gets more complex.

1
2
3
4
5
6
7
8
9
10
# Compact
status = "Approved" if score > 80 else "Pending" if score > 50 else "Rejected"

# Clearer
if score > 80:
    status = "Approved"
elif score > 50:
    status = "Pending"
else:
    status = "Rejected"

There’s no performance gain in the compact form. The only thing you save is vertical space—and that’s not worth trading for mental overhead.


3. Don’t Abuse One-Liners

Do whatever your eyes find the structure faster. It’s not about pedantry—it’s about being kind to your readers (again: future-you is a reader, too).

🐞 Harder to Debug

Another underrated cost of one-liners? They’re harder to step through in a debugger.

If something goes wrong inside a dense one-liner, you don’t have access to the intermediate values. You can’t set breakpoints easily or inspect what’s happening mid-expression. You often end up rewriting the line just to see what went wrong—which defeats the whole purpose of trying to be “efficient” in the first place.


4. Descriptive Variable Names Win

1
2
3
4
5
# Compact but confusing
d = {i: i*i for i in range(10)}

# Clearer
squares_by_number = {number: number**2 for number in range(10)}

A few more characters can make the intent jump off the page. And no one has ever complained that code was “too easy to understand.”


5. Explicit Is Better Than Implicit

Compare these:

1
2
# Compact but unclear
data = json.loads(input_data) if isinstance(input_data, str) else input_data
1
2
3
4
5
# Clearer
if isinstance(input_data, str):
    data = json.loads(input_data)
else:
    data = input_data

You may be saving a line, but you’re making the reader mentally simulate what happens in both branches. That cognitive tax adds up.

The second version tells you exactly what it’s doing. No guesswork required. It lives up to one of Python’s core values: explicit is better than implicit.


Final Thoughts

There’s a kind of ego in compact code. A pride in writing something that “just works” in as few lines as possible. But Python invites a different kind of pride: the kind that comes from code that reads like prose, that communicates your intent without needing comments or explanations.

If you’re writing code for humans (which you are), prioritize clarity.

Leave clever for puzzles. Leave compact for compression algorithms.

In your everyday code?

Let it breathe.


If you’ve ever found yourself rewriting a one-liner just to debug it—welcome to the club.

A real example I faced once (unfortunately):

1
2
return sorted(reduce(lambda x, y:x.union(set(y)), map(lambda x:torange(x, n), map(lambda x:x.split(':'), ranges.split(','))), set()))

This post is licensed under CC BY 4.0 by the author.