Skip to main content

Command Palette

Search for a command to run...

The Forgotten Art of Immutability: Why Your Variables Should Stay Put

Updated
3 min read

Confession time: Python turned me into a slob. After years of writing strict, spotless C++ where const guarded everything like a loyal watchdog, I fell into Python’s cozy world and started treating variables like sticky Post-it notes: reuse them, scribble over them, hope for the best. Then one day I realised I was casually overwriting variables all over my functions. My past self would have screamed. That was my wake-up call: immutability matters, a lot.

The Single Assignment Principle

There’s one tiny rule that can massively boost your code quality: give a variable one value, once, and then leave it alone. Unless you’re doing actual iterative math in a loop, treat variables as write-once, read-many treasures. This isn’t some ivory-tower purity test, it's survival wisdom forged in the fires of 2 AM debugging sessions.

Picture this: you're stepping through a debugger, and every calculation you ever made is still there, neatly preserved. You can follow the trail from userAge to adjustedAge to finalAge like breadcrumbs through the forest. Overwrite those variables, and poof; the trail vanishes. Now you’re staring at a single variable that’s been shapeshifting like a superhero.

The real nightmare hits when you refactor. You extract a chunk of code, move it somewhere else, and suddenly that reused variable name either doesn’t exist anymore or means something totally different. The code breaks silently, and you spend hours hunting a ghost bug that never would’ve existed if you’d created userAge2 instead of bulldozing userAge.

The Language Landscape

Languages all treat immutability differently. C and C++ give you const, which might honestly be one of the most powerful keywords ever invented. Marking almost every variable const should be the default. In fact, I wish the languages had flipped the rules: everything immutable unless you explicitly declare it mutable. Imagine the bugs that would never be born.

Functional languages like Haskell and Erlang go all-in: variables never change, full stop. It sounds harsh, but it wipes out entire classes of state-related bugs before they can bite. Rust takes a similar stance (immutability first) forcing you to mark variables as mut when you really mean it.

JavaScript tried to clean up its act by adding const and let to replace the chaotic var, though const still lets you mutate objects under the hood. Scala and Kotlin offer val (immutable) and var (mutable), gently steering developers toward safer patterns without yelling about it.

Then we have Python, beautiful, flexible, dangerously forgiving. It hands you a flaming torch and says, "Try not to burn down the house." No built-in immutable variables, just conventions and discipline. Turns out I needed to relearn both.

The Bottom Line

Immutability isn’t about being a zealot, it’s about being kind to future you. When every variable has a clear purpose and never secretly changes, your code becomes simpler to read, easier to debug, and far safer to refactor. Some languages enforce this for you, others leave you on your own, but the lesson is universal: variables that stay put lead to programs that stay solid.

More from this blog

A

A Blog about Software Development

96 posts