Andy Hawthorne

Avoid clever code

Clever code requires even higher level debugging. And that makes life hard...

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. It might need to as it expands to meet the need, 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 (The C Programming Language). He is often quoted, 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.

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 know debugging happens for the main framework code. Or, maybe we will debug it by using it - we'll get to that.

The point here is: it's easy to write clever code. As you write the code you need to meet the functionality requirements, you can get carried away. And you can be even more clever when you refine your code.

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.

Unit testing generates a lot of noise around the developer community. And it does make a lot of sense. Frameworks are unit tested by the creators (usually). And bugs will get 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 is hard. Especially when your brief is anything but simple. 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.

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 with complexity is not efficient. It makes said code hard to work with. And it'll be hard to debug and maintain. Aim for elegant simplicity instead. Or at least write code that achieves it's purpose through efficiency, not cleverness.