Kernighan's Law

Kernighan’s Law says that debugging requires understanding what the code actually does, which can be twice as hard as writing it. When coding, you operate with a specific mental model and full context. When debugging, you might be dealing with someone else’s code or your own code after that context has faded.

Writing “clever” or complex code is essentially setting a trap for your future self. A maintainable version is usually superior to an optimized version that is difficult to understand. As Kernighan implies, if you make your code too tricky, you’ve essentially outsmarted yourself.

  • Bug detection and removal is more complex than programming because debugging requires understanding both the code and why it doesn't work.
  • If you write code at the limit of your intelligence, you won't be able to understand or troubleshoot it later.
  • Simple code with good structure and documentation is easier to debug, saving time in the long run.
  • Even if your code runs successfully when you write it, it's fragile if it's too complex.
添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论