Code and words

Why Clever Coding Is a Bad Idea

November 25, 2019 , posted under Coding
Clever code makes maintenance and debug almost impossible. Here's why...

There’s always another bug. It’s a fact of life in any discipline that requires writing code. There is nothing wrong with a piece of code getting more complex as it expands to meet the requirement, but it’s what you start out with that’s important.

Brian Kernighan is a coding hero. He co-authored the best programming manual ever written. He has been quoted many times, but this one supports the argument I’m making here:

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
Brian Kernighan

So let’s think about that for a moment. Let’s think about in the context of web development.

Don’t we all use frameworks these days?

That’s probably true, frameworks play a big part in how we do web development. We still end up writing code to build our web app/site, though.

It’s that code that we are talking about. We have to assume the framework code we are using has already been debugged. Or, maybe we will debug it by using it - we’ll get to that.

The point here is: as you begin to write the code you need to meet the functionality requirements, it’s easy to be clever. And you can be even more clever when you refine your code further.

This situation is framework agnostic. It’s more to do with how individual developers write code. That means the choices you make as you write, are directly linked to how difficult you code will be to debug.

A lot of noise is made about unit testing. And it does make a lot of sense. Frameworks are unit tested by the creators (usually). And bugs are found, reported and squished by people using said framework.

Imagine how hard that would be, if the framework contained masses of clever code.

It has to be clever - there’s are many complex requirements

Writing code that conforms with simplicity when your brief is anything but simple, is hard. But that’s why we are developers. We develop solutions for difficult problems.

Our code doesn’t need to get so clever (complex) that debugging becomes impossible. We have programmatic building blocks to help. Classes, functions etc. can be used to make sure our code answers the question - but can also be debugged.

While I’m here, I’ll throw another Kernighan quote into the mix:

Controlling complexity is the essence of computer programming.
Brian Kernighan

Exactly. That’s what I’m saying. When we write web code it may not be programming as such, not in the strictest sense. But the principles still apply. Keep it clean, keep it simple, let it evolve.

What’s with the debugging obsession?

It might sound old school to talk about debugging. But even if you only ever write code and test in the browser, you’ll still be trying to squish bugs. That’s debugging. It’s when stuff doesn’t work as expected.

The bottom line…

Writing code cleverly is not efficient. It makes said code harder to work with in terms of debugging and maintenance. Aim for elegant simplicity instead. Or at least write code that achieves its purpose through efficiency, not cleverness.