Only A Stupid Programmer Would Try To Be Clever


It takes an insecure jerk to write a clever line of code like this:


What does it say? If X is true, set X, Y, and Z to 0, but if X is false, set X and Y to Z+2. It’s a shortcut written by someone who thought they were being clever. To an experienced programmer it’s readable, but still a little uncomfortable to deal with if you’re trying to trace a bug. The longhand version would be something like (note descriptive variable names):

if( LeftPosition != 0 )
LeftPosition = 0;
MiddlePosition = 0;
RightPosition = 0;
LeftPosition = RightPosition + 2;
MiddlePosition = RightPosition +2;

(Irritating failure to indent courtesy of WordPress.)

It’s longer to type, but a casual reader is more likely to be able to understand the code at first glance without having to re-read or decipher it.

If you read through any sizeable codebase you’ll find a ton of things done just for the sake of being “clever”. A lot of these are shorthand or obfuscation that has no real purpose since the compiler will generate the same executable code in most cases.

I think it boils down to insecurity. Programmers who don’t have much confidence in their abilities try to be clever and show off. Skilled programmers know a fundamental point about writing code:

Not everyone is as smart or skilled as I am, so I need to make this as simple and readable as possible.

Keeping this point in mind often results in code that is easier to debug, better-commented, and simpler to read.

Cleverness is quite possibly the number one reason why people prefer to scrap and rewrite a codebase rather than fix the problems in it. Code is easier to write than it is to read, and if it’s made all that more unreadable by an ineloquent programmer, then it’s less likely to stand the test of time.

Be a real programmer. Write simple, clear, understandable code. You will have to read your own code someday. Do you really want blood to shoot out of your eyes when you try to read it?

For further reading, check out the infamous “How to Write Unmaintainable Code”.