Clean Code is Easier to Debug

by chromatic

In a recent discussion on lkml (found thanks to LWN, Ingo Molnar defended checkpatch, a Perl program which reviews proposed patches against the kernel's coding standards. In particular:

you might know that Deja-Vu moment when you look at a new patch that has been submitted to lkml and you have a strange, weird "feeling" that there's something wrong about the patch.

It's totally subconscious, and you take a closer look and a few seconds later you find a real bug in the code.

That "feeling" i believe comes from a fundamental property of how human vision is connected to the human brain: pattern matching. Really good programmers have built a "library" of patterns of "good" and "bad" looking coding practices.

If a patch or if a file has a clean _style_, bugs and deeper structural problems often stand out like a sore thumb. But if the code is peppered with random style noise, it's a lot harder (for me at least) to notice real bugs. I can notice bugs in a squeeky clean code base about 5 times easier than in a noisy codebase. This effect alone makes checkpatch indispensible for the scheduler and for arch/x86.

...

I've yet to see a _single_ example of a good, experienced kernel programmer who writes code that looks absolutely careless and sloppy, but which is top-notch otherwise.

I've spent a lot of time this past year cleaning up a medium-sized codebase written in cross-platform C89 (c'mon vendors, can't you upgrade your compilers tosupport C99 already? I know Microsoft has a few programmers on staff.). Coding standards help immensely.

Even something as simple as aligning the equals signs in assignments and separating paragraphs in code with newlines increases skimmability greatly. Ingo's right though, and I wish I'd realized it consciously earlier. Consistent code requires much less brainpower to decipher its structure, leaving that much more brainpower to find real problems.

While there may never be a refactoring called "Beautify code block" (and technically that's not improving the design of the code), it's an important tool for human analysis of code.


5 Comments

Andy Lester
2008-02-29 07:01:05
I also suggest that ugly code gets read less, lowering the chances that it will be tended to.
chromatic
2008-02-29 10:34:46
Perversely, ugly code may also get reimplemented more.
Phlip
2008-03-04 02:12:41
Code with unit tests is easy to clean.


Clean code is easy to test.


Clean, tested code resists bugs, and _prevents_ debugging.


Give me a vicious cycle like that! C-:

Vern Bliss
2008-03-04 14:34:46
Right on! It not only helps to find bugs but makes the code much more maintainable in all ways. You would be surprised (probably not) at how hard it is to convince many programmers and engineers that writing readable code is worth the effort.


retired Boeing software engineer

Bobo
2008-04-29 03:02:09
Was it Kernighan who said, since it's twice as hard to debug, you should never code at > 50% cleverness?