Why Perfect Code Is a Myth and How to Embrace Pragmatic Refactoring
The article argues that striving for flawless code is futile because most software constantly evolves, and it advocates focusing on correctness, readability, and pragmatic refactoring rather than chasing unattainable perfection.
Software systems often evolve over many years, while individual lines of code or designs may only last days or weeks; some code is rewritten repeatedly, and a small portion remains stable for the system’s lifetime.
Michael Feathers identified the "code lifecycle" concept, noting that most systems contain a large amount of code written once and never changed, but a critical subset is modified, refactored, or rewritten many times.
Understanding which code will change frequently allows developers to predict importance and allocate effort wisely.
While clean, consistent, and simple code is essential, obsessively pursuing elegant or perfect code can be counter‑productive when that code is expected to change often.
"You cannot write perfect code. Accept this as a principle and celebrate it, because perfect code does not exist in the brief history of computing," – Andrew Hunt, *The Pragmatic Programmer*.
Code that is written once should prioritize correctness, readability, and maintainability; excessive compactness or elegance is unnecessary, and limited duplication is acceptable.
Conversely, code that is continuously modified should not be over‑engineered; spending time on style or optimal solutions for such volatile code wastes effort, as it will be altered or replaced soon.
Key quality criteria include functional correctness, usefulness, efficiency, error handling, safe failure, debuggability, and ease of safe modification—not visual appeal.
The core of lean development is to avoid wasting time on unimportant tasks, guiding how we write, refactor, review, and test code.
Adopt opportunistic (or “boy‑scout”) refactoring to keep code simple and safe without over‑refactoring code that isn’t being changed.
During code reviews, focus on correctness, defensive programming, safety, understandability, and safe modification rather than stylistic debates.
Let IDEs handle formatting; avoid arguments about object‑oriented purity unless teaching novices.
Testing is vital; write effective test cases that cover main execution paths and critical exceptions, regardless of whether they are large‑scale or small‑scale, written before or after the code.
Software differs from civil engineering: we build highly mutable, abstract artifacts whose primary purpose is to be changed. As such, code should be seen as a temporary representation of a solution, not a permanent masterpiece.
"After five years of use and modification, a successful software source often looks completely different, whereas a successful building remains largely unchanged," – *Sustainable Software Development*.
Recognize that code is a transient artifact reflecting problem understanding and design, not the ultimate value of a product.
"We need good code: understandable, correctly running, and secure. Yet we must remember that some or all of this code may soon be discarded or never read again, so we should optimize our effort and avoid chasing perfect code," – Dan Grover, *Coding and Creative Destruction*.
Iterative development teaches us to constantly test whether our solutions solve the problem, adapt when they don’t, and accept that even correct designs may become obsolete as requirements evolve.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Baidu Tech Salon
Baidu Tech Salon, organized by Baidu's Technology Management Department, is a monthly offline event that shares cutting‑edge tech trends from Baidu and the industry, providing a free platform for mid‑to‑senior engineers to exchange ideas.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
