[KongchangAI]
· 2 min read· 1,061 words

Why Writing Correct Software Is So Hard

Why Writing Correct Software Is So Hard

Software is hard to write correctly because complexity grows nonlinearly; it requires active management, not just testing.

The article re-examines software correctness from a complexity perspective, arguing that the root cause is the nonlinear growth of state spaces and interaction paths beyond human cognitive capacity as systems scale, not programmers' lack of effort. Locally correct modules can still fail when combined due to interface assumptions, timing dependencies, and error propagation, problems nearly invisible at the unit test level. The Hacker News discussion diverges on tools: some favor type systems and formal methods, others stress their costs and limits; a compromise is to allocate a correctness budget by risk. The author's practical advice is to treat complexity as an engineering metric for active management, combating it at the source by reducing states and converging interfaces.

Why Writing Correct Software Is Hard

Software correctness is an old problem, yet it has never truly been solved. This article on pron.github.io, which was pushed to 303 points on Hacker News, re-examines the question of "writing code correctly" from the perspective of complexity, and the 131 comments in the discussion section also confirm how much it resonates with practitioners.

Core Thesis: Complexity Is the Natural Enemy of Correctness

The article's fundamental judgment is this: the reason software is hard to write correctly is not rooted in programmers' abilities, but in the fact that the complexity of the problem itself grows nonlinearly with system scale. When state spaces, interaction paths, and boundary conditions stack up, the range within which the human brain can reliably reason is quickly overwhelmed. This means that "trying harder and being more careful" cannot fundamentally solve the problem—complexity grows faster than human cognitive capacity.

From an engineering perspective, this judgment explains why formal verification, type systems, and test coverage each have value yet none can alone guarantee correctness. They merely compress complexity from different directions rather than eliminate it.

State space explosion is a key concept for understanding the nonlinear growth of complexity. The total number of states a system can be in equals the product (not the sum) of the state counts of its components. For example, 10 boolean flags each with only 2 states combine into a state space of 2^10 = 1024; as the number of components increases linearly, the state space expands exponentially. The human brain's working memory can handle roughly only 4-7 information chunks at once, so once the state space exceeds a certain threshold, programmers effectively cannot exhaustively enumerate all paths in their heads and must rely on abstraction and local reasoning. This also explains why "more careful code review" has limited effect: reviewers are equally constrained by cognitive bandwidth and cannot reliably cover exponential combinations.

Formal verification refers to techniques that use mathematical methods to prove that a program satisfies a given specification; common approaches include model checking, theorem proving, and Hoare logic. In the ideal case it can provide a strong guarantee that "no counterexample exists," but the cost is the need for precise formal specifications, considerable proof engineering effort, and limited scalability for concurrent and infinite-state systems. This directly corresponds to the article's judgment that "tools compress rather than eliminate complexity": formal verification shifts the correctness burden from testing to specifications and proofs without eliminating the complexity of the problem itself.

Why Local Correctness Cannot Guarantee Global Correctness

The article emphasizes an easily overlooked fact: every module may pass its tests individually, yet the combination can still fail. Interface assumptions between modules, timing dependencies, and error propagation paths constitute a large number of failure modes that are invisible at the unit level.

The trouble with such problems is that they often surface only under specific combinations, which conventional testing can hardly cover. Some developers in the comments noted that the root cause of production incidents is often "two individually correct components producing erroneous interactions under specific states"—exactly the typical failure from the complexity perspective.

Combinatorial explosion here refers to the following: if a system has n modules and each module has k behavior modes, then the number of cross-module interaction combinations can reach the order of k^n. Unit tests verify only the behavior within a single module and have essentially no coverage of inter-module interaction paths. This is precisely why "inconsistent interface assumptions" become a major source of failure—each module works correctly according to its own understanding of the contract, but the discrepancy between two understandings only manifests under specific combinations.

Race conditions are a typical representative of timing-dependent failures: when multiple concurrent execution units access shared state in an unpredictable order, the final result depends on scheduling timing. The difficulty with such defects lies in their non-reproducibility—the same piece of code may always take the "lucky path" in a test environment and only be exposed under production load. This aligns closely with the article's emphasis on "erroneous interactions under specific states" and also explains why coverage metrics cannot be equated with correctness guarantees.

Divergence in the Discussion Section: How Much Can Tools Save

Among the 131 comments on Hacker News, there is clear disagreement on "how to respond." One side believes that stronger type systems, formal methods, and static analysis can push correctness a big step forward, especially in critical systems; the other side points out that these tools themselves have learning costs and applicability boundaries, making them hard to fully adopt under business pressure.

A compromise idea repeatedly mentioned is: do not pursue global correctness, but identify the highest-risk parts and invest verification costs there. This approach of "allocating a correctness budget by risk" is more realistic than uniformly requiring formal verification.

Implications for Everyday Development

The value of the article lies not in offering a silver bullet but in changing the way we ask questions. Rather than asking "how do I guarantee this code is correct," ask "where does the complexity of this code come from, and can it be reduced first." Reducing states, converging interfaces, and narrowing the scope of mutability—these seemingly plain measures are precisely how to combat complexity at its source.

For teams, treating complexity as an engineering metric that needs active management, rather than merely an object of post-hoc testing, may be the most practical takeaway from this article.

One-line Summary

Software is hard to write correctly because complexity grows nonlinearly; it requires active management rather than testing alone.

Paragraph Summary

The article re-examines the software correctness problem from a complexity perspective, pointing out that the root cause lies in the nonlinear growth of state spaces and interaction paths as systems scale beyond human cognitive capacity, not in programmers' lack of effort. Modules that are locally correct can still fail when combined due to interface assumptions, timing dependencies, and error propagation, and such problems are nearly invisible at the unit test level. The Hacker News discussion section diverges on the value of tools: one side favors type systems and formal methods, while the other emphasizes their costs and boundaries; a compromise is to allocate a correctness budget by risk. The author's practical advice is to treat complexity as an engineering metric for active management, combating it at the source by reducing states and converging interfaces.

#software correctness#complexity#formal verification#type systems#state space explosion#race conditions
Share:

Related articles