I was reading John Sonmetz’s post 11 Rules All Programmers Should Live By and I wanted to expand on 2: Clever is the enemy of clear.
In some cases, you do need to write clever code. If you’re working on something that absolutely must be optimized as much as possible, fine, get clever. If you’re working on something that’s actually hard, like cache invalidation, knock yourself out. But if you’re working on plain old business software like probably 99% of coders out there, knock it off! In almost all cases clever code is just so much mental masturbation: all it accomplishes is making the coder feel good about how very clever they are. Except that writing clever code is actually stupid.
Why? Because the point of writing code is to solve a problem. Clever code, on the other hand, is a problem. Remember, the vast majority of development is maintenance, not greenfield development. It’s much, much more important for other people to be able to understand and modify your code than for you to gain whatever performance or flexibility you think your unmaintainable code is going to get you.
Your job is not to congratulate yourself on how clever you are, it’s to add features so the company can make money, fix bugs so the company can make money, pay down technical debt so that adding features and fixing bugs is easier and the company can make money, automate tedious stuff so humans can do more interesting things that make the company money, etc, etc. And yes, this advice obviously applies to open source as well. At a company, you can make people work on code they hate by paying them. In open source, not so much. If people don’t want that bugfix/new feature badly enough to tolerate the mess you made when you decided to be clever, they’ll remember they could also be doing literally anything else, and then go do that instead. I’ve seen code that made scrubbing the floor sound like a good time in comparison, and if I didn’t have to work on it I most certainly would not have.
Not only that, but clever code doesn’t even make you look clever. Anyone who can see the bigger picture or has ever done any maintenance thinks you’re a jerk and probably a dumb jerk at that. Like Einstein said, “If you can’t explain it simply, you don’t understand it well enough.” If you can’t code it simply, either you don’t understand it well enough or you’re not a good enough designer to make it simple. Do you really want to convince the next people who work on your code that you don’t know what you’re doing?
Clever code actively harms your codebase, slows development, and makes the people who maintain your code want to throw chairs at you. Just say no to clever code.