Simplicity is achieved through reduction

“Do it again…” my boss said, firmly holding down the Delete key with his finger. With familiar sorrow, I watched the screen where my code was irrevocably disappearing, line by line.

My boss Stefan wasn’t particularly eloquent, but he could spot bad code at first glance. And he knew exactly what to do with it. I took that job as someone learning programming – full of energy and enthusiasm, but with no idea how to write code.

I was under the terrible delusion that any problems could be solved by adding another variable in the appropriate place or by adding another line of code. On bad days, my code would degrade—its logic wouldn’t improve, and moreover, it would become bulkier, more complex, and more unstable.

The desire to solve a problem with minimal changes to a block of code—even if they are terrible—is quite natural, especially when time is short. Most programmers keep bad code, fearing that if they start over, it will require much more effort than simply going back to the beginning. This is true for almost working code, but there is also code that nothing can help anymore.

Too much time is wasted trying to save a bad job. If something starts draining resources, it should be eliminated. And quickly.

I don’t want to say that it’s so easy to part with the typed text, chosen names, formatting. My boss’s reaction was overly harsh, but it made me rethink my code during the second (and sometimes third) attempt. Nevertheless, the best way to fix bad code is to be prepared to ruthlessly rework it, move it back and forth, or delete it.

The code should be simple. The number of variables, functions, declarations, and other syntactic elements of the language should be minimal. Extra lines, extra variables… everything unnecessary should be immediately eliminated. What remains should be just enough to complete the task, implement the algorithm, or perform the calculations. Everything else is useless, unnecessary noise that appeared by mistake. It confuses the algorithm and hides the truly important things in the code.

Of course, if this is not enough, delete everything and start over. Such “drawing from memory” often helps to get rid of unnecessary clutter.