RSSAmplifier

Laws of Software Engineering · Jun 24, 2026

Kernighan's Law

0
Sign in to vote or save

Dr. Milan Milanović · Laws of Software Engineering

2 min read

Debugging is twice as hard as writing the code in the first place.

Takeaways

  • 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.

Overview

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.

Kernighan's Law illustration

Kernighan’s Law

Examples

Imagine a developer writing a function in a compressed style, chaining multiple operations in one line:

public string GetUserDisplay(User u) =>
     u?.IsActive == true ? (u.Name ?? "").Trim() is var n && n.Length > 0
     ? n + (u.Role > 0 ? $" ({(Role)u.Role})" : "") : "Unknown" : "Inactive";

The proper, readable version:

public string GetUserDisplay(User user)
{
    if (user is null || !user.IsActive)
        return "Inactive";
    var name = user.Name?.Trim();
    if (string.IsNullOrEmpty(name))
        return "Unknown";
    if (user.Role > 0)
        return $"{name} ({(Role)user.Role})";
    return name;
}

The clever version might have taken 30 minutes to write, but debugging took 3 hours. Had the code been written clearly, it would’ve taken 45 minutes to write, but only 30 minutes to debug later.

Origins

Brian Kernighan first expressed this idea in The Elements of Programming Style (1974, second edition 1978) with P.J. Plauger. Kernighan, famous for co-authoring “The C Programming Language” and “The Elements of Programming Style,” wrote about simplicity in the days of resource-constrained computing.

Further Reading

Want to go deeper?

All 63+ laws are covered with more depth, examples, and practical guidance in the Laws of Software Engineering book.

Get the Book

Last updated: June 24, 2026

Read the original on lawsofsoftwareengineering.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.