Lately I’ve found satisfaction in following a minimalist lifestyle. Having a lot of stuff isn’t all it’s cracked up to be and more often than not distracts me from how I really want to spend my time. My desk has been described as “spartan” and led to more than one mention that a clean desk is a sign of insanity. (Is it really? That’s a discussion for another day.)
The idea behind minimalism is that you only keep what you need, and any time you consider adding to your collection of things, question whether it’s actually needed. Anything beyond what’s necessary is clutter; moreover, it’s a distraction that requires energy to maintain. This idea applies to personal organization and consumerism, but also to development.
Python is a great language for aspiring minimalists. The Zen of Python could easily double as the zen of minimalism given minor edits. “There should be one — and preferably only one — obvious way to do it” is minimalist gold.
It’s easy for code to get messy. Our brains don’t think like computers (if yours does, you might be an autonomous robot), and ideas translated into code often come out jumbled. Add more than one brain and the jumble grows exponentially. But if we build in some time to revisit and question how we wrote something, we can improve readability, maintainability and often the performance of our code.
For example, during a recent refactor of my own hastily-written code, it took me multiple glances — and scribbling down a truth table — to realize that this:
if len(list1) > 0 and len(list2) > 0:<br /> setting1 = True<br /> setting2 = True<br /> else:<br /> if len(list1) > 0:<br /> setting1 = True<br /> else:<br /> setting1 = False<br /> if len(list2) > 0:<br /> setting2 = True<br /> else:<br /> setting2 = False
is logically equivalent to this:
setting1 = (len(list1) > 0)<br /> setting2 = (len(list2) > 0)
I’m sure the first way made sense as I was writing it; I was probably adding conditions one at a time as they were needed. But I should have gone back before committing my changes and noticed something like that.
During the same refactor, a more broad set of changes reduced six Django views down to two, eliminating redundant database calls, making it easier to implement a Django form and removing almost all need to store data in session. Refactoring felt good, but it should have been done that way in the first place. Keeping a minimalist mentality from the start would have made my refactoring unnecessary.
And so I’ve lately been slowing down as I code. I run through a mental checklist as I go and proofread every changed line a few times before I commit it. It may take more time now, but I can save my team time later if the code is easier to modify and maintain. I’m continually asking myself questions like these:
- Is every variable in this function necessary? Is anything redundant?
- Could a list comprehension replace this loop?
- Could an inline conditional replace this
- Does an existing library or module already do this?
- Could I use or extend a utility function to handle this?
- Can these similar database calls be reduced into one transaction without being confusing?
- Can some simple functional programming reduce a few lines of code?
Tell me what you think. Can you apply any of these questions to your code? Is there anything I’ve missed? I’d love to hear what’s working for other minimalists out there.