Kernighan’s law

Kernighan’s law states: 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. It is attributed to Brian Kernighan, co-author of The C Programming Language and The Elements of Programming Style, where a version of the observation appears.

The law is an argument for restraint in how cleverly code is written. A compact, dense solution that stretches the limits of a programmer’s own understanding when writing it will, by the law’s logic, exceed that same programmer’s understanding when they later have to find a bug in it under pressure and without the full context they had while writing it the first time. Debugging is a harder cognitive task than authoring, so code should be written with a safety margin below the limit of what its author can follow.

The law is one of the standard justifications for clean code, and for preferring the KISS principle's plain, obvious solution over a terser or more "elegant" one. It is frequently paired with the related observation that code is read far more often than it is written, so optimizing for the reader, including one’s future debugging self, tends to pay off more than optimizing for brevity at the point of writing.

See also

References

  • Kernighan and Plauger (1978). The Elements of Programming Style (2nd ed.). McGraw-Hill.