Refactoring and Legacy Code
From small refactorings and the right attitude to inherited code to clean code, naming, automated upgrades, code review and technical debt, the care of existing software as told through the free samples of Leanpub books.
Created from the free samples of 9 Leanpub books and the descriptions and tables of contents of 24 more books and courses, with every source cited.
Refactoring is the rewriting of working code to make it more readable, reusable and maintainable without changing what it does.[3] It is the chief means of dealing with legacy code, which one of the sources defines from the programmer's side as code that is hard to understand, uncomfortable to change and nonetheless one's own concern.[1] Three neighbouring subjects belong with them: clean code, the qualities that refactoring aims at; code review, where most problems are first noticed; and technical debt, the name for what accumulates when the work is put off.
The case for all of it is economic. Maintenance is estimated to take 60 to 80 per cent of the cost of software over its life, and roughly half of that effort goes into understanding the code that is to be changed.[5] The larger a codebase grows, the more maintenance it needs.[2] Nor is legacy code a misfortune of particular companies: a programmer who leaves one employer because of it will find it at the next.[1]
This article describes refactoring, legacy code, clean code and naming, automated refactoring, code review, and technical debt and modernisation. Its sources are the free samples of 9 Leanpub books and the public descriptions and tables of contents of 24 more books and courses, published between 2015 and 2026.
Where to start
A newcomer to refactoring has three short books. Refactoring Tweaks (2016) explains what refactoring is and why it pays before giving a set of small techniques; it is written for people who build WordPress code and says its techniques apply in any language, and Refactoring Tweaks - Workbook (2016) adds practice exercises.[3] [4] Refactoring TypeScript (2019) takes one cause of unhealthy code per chapter and pairs it with a remedy, and 10 Refactorings to Boost your Clean Code Efficiency (2022) walks ten common refactorings through a single small example.[11] [12]
A developer who works in an inherited codebase every day is the reader of The Legacy Code Programmer's Toolbox (2022), which begins with the attitude to take and goes on to techniques for understanding code one did not write.[1] Refactoring to Clean Code (2018) gives a staged plan for a codebase in poor condition, and Naming Things (2023) treats the one skill of choosing names.[2] [5]
For reviewing other people's code, What to Look for in a Code Review (2016) is a short collection of tips and Code review mini guide (2026) addresses junior and intermediate developers.[10] [27] Two books assume more. The Other Half of Coding (2026) is for experienced developers who already practise test-driven development and refactoring and still find change harder than it should be, and The Adaptable Enterprise (2026) is written for architects and engineering leaders.[9] [32]
What refactoring is
The definition has two halves, and Refactoring Tweaks insists on the second: the code works before and works the same way after. What changes is its quality, which the book reduces to three properties, readability, reusability and maintainability, because those drive what software costs over its whole life.[3] Readable code says what it is doing and why without needing comments, so that a colleague can open it six months later and find the way. Maintenance is expensive and risky, since fixing one fault can cause another, and the book's starting premise is that all code, everyone's, needs improving.[3]
Refactoring proceeds in small steps, each a named change. Refactoring TypeScript compares neglected code to a garden overgrown with weeds and organises itself by weed: null checks everywhere, wordy and nested conditionals, long methods, each identified and then met with techniques such as combining conditions or extracting a method.[11] 10 Refactorings to Boost your Clean Code Efficiency carries out its steps with the shortcuts of a development environment, slice by slice.[12] The workbook to Refactoring Tweaks turns the activity into an exercise of four steps on real snippets of code: review the code, identify each problem area, decide why it could be better, and rewrite it.[4]
When to do it is part of the discipline. Refactoring to Clean Code observes that developers routinely copy some code, change it to suit and test the result, and that this is harmless only if a fourth step follows before the change is committed, which is to refactor; leaving that step out is how duplicated code piles up.[2]
A plan for a codebase in poor condition
Refactoring to Clean Code proposes three stages for a codebase weighed down by neglect: quick wins that carry little risk, then dividing the code into components, then wrapping the components in automated tests.[2] The first quick win is to delete dead code, the functions never called and variables never read. Dead code enlarges the codebase and so its maintenance, wastes the hours of anyone who tries to understand it, and can be dangerous: the book retells how a trading firm lost $440 million in 45 minutes when a changed flag brought back to life a piece of code that had been dead for years.[2] It can be found with static analysers, by looking for files nobody has touched for a long time, which are either right, dead or too frightening to change, and by monitoring which lines actually run in production. In the author's experience teams removed 4 to 7 per cent of their lines within two or three days.[2]
The second quick win is duplication. The book distinguishes four kinds of clone: exact copies, copies with renamed identifiers, copies with a line or two changed, and code that does the same thing in a different way. The third kind repays inspection, because the changed line is often a bug that was fixed in one copy and not in the other. Duplication is costly for a reason the book takes from a study of maintenance spending, in which most of the effort went into locating the faulty code; every copy of a fault has to be found again.[2]
Legacy code
Definitions of legacy code vary. The Legacy Code Programmer's Toolbox lists three in circulation, code without tests, code tied to technology that is no longer supported, and simply code written before one arrived, and proposes its own three-part definition from use: hard to understand, uncomfortable to change, and one's concern. By that definition one person's legacy code is another's ordinary code.[1] The book notes that nothing in a programmer's training prepares for it, since students and hobbyists build small projects from nothing, while professional work starts from existing code that has to be understood before anything can be added.[1]
Its first chapter is about attitude. The natural reaction to tangled code is to conclude that whoever wrote it was incompetent. The book answers that the code in question earned the customers that pay today's salaries; that it was written with the knowledge and tools of its time; and that it is rarely the work of one person, being the accumulation of changes by many people who each understood the design slightly differently.[1] Its advice is not to complain about code one does not intend to improve, because complaint spreads through a team, and to take ownership of the code one works on, which is different from taking the blame for it. It adds a caution against the feeling of superiority: the best code written today may be laughed at in ten years, and a programmer who finds nothing to improve in code written a year ago has not learned much since.[1] The rest of the book covers ten techniques for understanding unfamiliar code, the spreading of knowledge in a team, finding the source of a bug, and deciding when to refactor and when to leave code alone.[1]
Other sources ask why code becomes legacy. Recipes for Decoupling (2022) blames coupling: projects that followed their framework's way in everything were fast to build and ended with their business logic scattered through controllers, queries and templates, so tangled that a migration was too hard and the application was written off. The remedy it proposes is loose coupling, with the domain logic in plain sight and the framework used in only a few places.[8] The Other Half of Coding starts from teams that adopted test-driven development, refactoring and design patterns and slowed down all the same, with areas of the code nobody wanted to touch, and argues that what they lack is a skill in carrying out change itself.[9] Modern Software Design in Fortran (2026) shows the same work on one of the oldest languages still in use, redesigning existing code through step-by-step refactoring.[25]
Clean code and naming
Clean code is the state refactoring aims at, and naming is its most frequent act. Naming Things gives the reasoning as a chain: good names make code understandable, and understandable code makes maintenance efficient. Identifiers account for about 70 per cent of a codebase, and a codebase is a store of knowledge about how an application should behave, so the ease of reading its names governs how quickly an engineer can absorb that knowledge. The book cites a study in which professional engineers paid more attention to the names of identifiers than to the comments in the source.[5] The book sets out four principles for a name, that it be understandable, concise, consistent and distinguishable, with rules under each.[5] A Short Guide to Naming (2026) treats readability as familiarity within a team and shows how context and scope decide the right length of a name.[13]
The general handbooks are large. Clean Code Principles and Patterns, 2nd Edition (2024) runs from architecture and object-oriented design through coding, testing, security and teamwork, with examples in several languages; Clean Code Principles And Patterns (2024) is its Python edition, and 140 Tips For Clean Code (2026) is the same author's collection of short tips.[14] [15] [16] Clean Code Fundamentals (2026) teaches software craftsmanship in Java, on the premise that nothing burdens a team so much as hastily written, illegible code, and Craft Your Code : Second Edition (2026) covers coding standards, names, comments and control flow across languages.[17] [18]
Others are written for one ecosystem. Washing your code (2024) shows front-end developers the code smells its author meets in daily code reviews and refactors each one; Clean JavaScript. English Edition (2021) is a concise guide to clean code, design principles and unit testing in JavaScript; and Master the Art of Writing Clean Code in C# (2018) is a course for C# on naming, clean functions and architectural smells.[19] [20] [21] For React, Maintainable React (2025) follows refactorings of example components from hard-to-change code to cleaner designs, and React Clean Code (2022) lists sixteen refactorings.[22] [23]
Automated refactoring
Some refactoring can be done by a program. Rector, the subject of Rector - The Power of Automated Refactoring (2025), reads PHP code into a syntax tree, applies rules to the tree and writes the changed code back, and is used to prepare a codebase for a new version of the language, to upgrade or change a framework and to raise code quality. The book says it has over 630 rules and teaches readers to write and test their own and to run the tool in continuous integration. It places the tool among three families of PHP tooling, those that enforce a coding standard, static analysers and tools for instant upgrades, and advises running the upgrade first and polishing with the coding standard afterwards.[7]
Its preface explains where the tool came from. One of the authors, then a trainer, taught a company the features of a new language version and was invited back a year later to find the project still not upgraded; the knowledge passed on in a day of training did not survive contact with a million lines of existing code. The tool began in 2017 as an attempt to make upgrades a matter of running a command.[7]
Rules can also keep a design from decaying. Each chapter of Recipes for Decoupling follows its refactorings with custom rules for a static analyser, run in the build, that reject code which would couple the application to its framework again.[8] Claude Code for Legacy Modernization (2026) applies an AI coding assistant to the modernising of critical systems. Its premise is that such projects rarely fail for want of new code; they fail because nobody can prove which behaviour matters, and so the book recovers hidden business rules and builds a means of verification before anything is replaced.[24]
Code review
Code review is the examination of a proposed change by someone other than its author before it joins the main codebase. Modern Code Review in the AI Era (2026) lists what it does at its best: catches bugs, security holes and design flaws while they are cheap to fix, spreads knowledge across a team, keeps style and architecture consistent, gives a place to discuss trade-offs, and teaches newcomers.[6] It is equally clear about what review is not: a gate for asserting authority, a substitute for automated tests and static analysis, or an occasion to argue about spacing. Treating it as any of these, the book says, makes engineers fear review and hide problems.[6] Its opening case is the faulty update from a security vendor that crashed about 8.5 million computers in July 2024, a simple logic error that had passed the vendor's own tests. It also cites research in which design and code inspections found 55 to 60 per cent of defects, a larger share than unit, functional or integration testing each found.[6] The book's second half concerns AI coding assistants, and its thesis is that they have made review more important, since the scarce skill is now evaluating code and not writing it.[6]
What to Look for in a Code Review, compiled from a series of blog posts, tells the reviewer what to check: tests, performance, data structures, design principles and security.[10] Code Reviews in Tech (2024) starts from three common difficulties, feeling daunted when one's own code is reviewed, lacking confidence in reviewing someone else's, and teams that cannot get reviews done in time.[26] Code review mini guide sets out how a team can put a review process in place and ends with a checklist, which the same author also publishes on a single page as The Code Review Checklist.[27] [28] SQL Code Review Mastery (2025) applies the idea to SQL in nine principles, from naming and formatting to security and testing.[29]
Technical debt and modernisation
Technical debt is the cost stored up by shortcuts in code, and refactoring is how it is repaid; Clean JavaScript. English Edition opens with the kinds of debt before it reaches clean code, and Dealing with technical debt (2015) is a booklet for agile teams on what debt is, how to reduce it and why bug counts rise.[20] [30] From Monolith to Modules: Refactoring Payment Providers in NestJS (2025) is a case study of one repayment: a week's refactoring of a payment system in which a single module holding more than a hundred registrations was split into modules that register themselves.[31]
Two short books address the people who decide whether the work is done. The Adaptable Enterprise argues that architecture fails when organisations design systems as if markets stood still, and proposes automated checks of architectural fitness, incremental migration away from legacy systems and the measurement of debt.[32] Unit Testing: Stop Shipping Garbage (2026) is addressed to engineering managers and describes the cycle in which tests are skipped at a deadline, debt accumulates, the build breaks in production and everyone agrees to do better next time.[33]
Questions about Refactoring and Legacy Code
What is refactoring?
What is legacy code?
Definitions vary, from code without tests to code written before one arrived.[1] The Legacy Code Programmer's Toolbox defines it as code that is hard for you to understand, that you are not comfortable changing and that you are nonetheless concerned with, so one person's legacy code can be another's ordinary code.[1]
Why not rewrite legacy code from scratch?
Where should I start with a codebase in poor condition?
Refactoring to Clean Code starts with quick wins that carry little risk: deleting dead code and removing duplicated code.[2] Dividing the code into components and wrapping them in automated tests come after.[2]
What is code review for?
Sources
Every source is a Leanpub book or course. The note after each says which part of it was read; where no sample was available, only the public description and table of contents were used.
- 1.The Legacy Code Programmer's Toolbox, Jonathan Boccara: Practical skills for software professionals working with legacy code Free sample: the introduction, with its definition of legacy code, and chapter 1 on the right attitude towards it. 2022. Free sample: PDF · EPUB
- 2.Refactoring to Clean Code, Amr Noaman: Concepts and Techniques for Taming Wild Code Free sample: the first stage of the roadmap, on removing dead code and duplicated code. 2018. Free sample: PDF · EPUB
- 3.Refactoring Tweaks, Tonya Mork: 7+ Easy Wins to Make Your WordPress Code Better & Increase Your Profits Free sample: the preface and the opening chapters on what refactoring is and why it matters. 2016. Free sample: PDF
- 4.Refactoring Tweaks - Workbook, Tonya Mork: Real World WordPress Code Challenges to Fine-Tune Your Refactoring Skills Free sample: the preface, with the four-step exercise process. 2016. Free sample: PDF
- 5.Naming Things, Tom Benner: The Hardest Problem in Software Engineering Free sample: the table of contents and the introduction on the purpose of the book and why naming is important. 2023. Free sample: EPUB
- 6.Modern Code Review in the AI Era, Steve Publications: A Comprehensive Guide to Building Better Software Through Collaborative Review and Intelligent Automation Free sample: the introduction, on why review matters, what it is and is not, and the effect of AI coding assistants. 2026. Free sample: PDF · EPUB
- 7.Rector - The Power of Automated Refactoring, Matthias Noback, Tomas Votruba Free sample: the preface on the tool's origins and the start of the introduction; most chapters appear as headings. 2025. Free sample: PDF · EPUB
- 8.Recipes for Decoupling, Matthias Noback Free sample: the introduction, on coupling and how to decouple efficiently. 2022. Free sample: PDF · EPUB
- 9.The Other Half of Coding, Max Guernsey, III: What they Didn't Teach You Free sample: the preface and the start of chapter 1. 2026. Free sample: PDF · EPUB
- 10.What to Look for in a Code Review, Trisha Gee: Effective tips for reviewing code No sample: from the description and table of contents. 2016.
- 11.Refactoring TypeScript, James Hickey: Keeping Your Code Healthy No sample: from the description and table of contents. 2019.
- 12.10 Refactorings to Boost your Clean Code Efficiency, Juntao Qiu: Small steps to make your code more readable. No sample: from the description and table of contents. 2022.
- 13.A Short Guide to Naming, Tim Ottinger: Understand how and why to better name modules, classes, functions, and variables. No sample: from the description and table of contents. 2026.
- 14.Clean Code Principles and Patterns, 2nd Edition, Petri Silen: A Software Practitioner's Handbook No sample: from the description and table of contents. 2024.
- 15.Clean Code Principles And Patterns, Petri Silen: Python Edition No sample: from the description and table of contents. 2024.
- 16.140 Tips For Clean Code, Petri K. Silén: Clean code is not an aesthetic choice; it is a professional advantage No sample: from the description and table of contents. 2026.
- 17.Clean Code Fundamentals, Martin Hock: Hands-on Guide to Understand the Fundamentals of Software Craftsmanship and Clean Code in Java No sample: from the description and table of contents. 2026.
- 18.Craft Your Code : Second Edition, Srihari Sridharan: Mastering the Foundations of Code Quality in the Era of AI Assistants No sample: from the description and table of contents. 2026.
- 19.Washing your code, Artem Sapegin: A book on clean code for frontend developers No sample: from the description and table of contents. 2024.
- 20.Clean JavaScript. English Edition, Software Crafters: A concise guide to learning Clean Code, SOLID and Unit Testing No sample: from the description and table of contents. 2021.
- 21.Master the Art of Writing Clean Code in C#Course, EngineerSpock: Learn how to write clean and maintainable code in C# No sample: from the description and table of contents. 2018.
- 22.Maintainable React, Juntao Qiu: Test-Driven Development, Refactoring and Clean Code. No sample: from the description and table of contents. 2025.
- 23.React Clean Code, Juntao Qiu: The secret to becoming a productive and clean coder. No sample: from the description. 2022.
- 24.Claude Code for Legacy Modernization, Thomas De Vos: Recover Hidden Business Rules, Build a Verification Harness, and Modernize Critical Systems Without Breaking Production No sample: from the description and table of contents. 2026.
- 25.Modern Software Design in Fortran, Matthias Noback No sample: from the description and table of contents. 2026.
- 26.
- 27.Code review mini guide, Adhir Ramjiawan No sample: from the description and table of contents. 2026.
- 28.
- 29.SQL Code Review Mastery, Luis Martin Espino Rivera: Write Clean, Secure, High-Performance SQL That Passes Every Review The sample has no readable text: from the description and table of contents. 2025. Free sample: PDF
- 30.Dealing with technical debt, Mario Lucero - Agile Coach - www.agilelucero.com No sample: from the table of contents. 2015.
- 31.From Monolith to Modules: Refactoring Payment Providers in NestJS, Oluwakemi Awosile: A Real-World Guide to Self-Registering Architectures and Cleaner Code No sample: from the description and table of contents. 2025.
- 32.The Adaptable Enterprise, Emeka Mbah: Evolutionary Architecture and the Strategic Management of Technical Debt No sample: from the description and table of contents. 2026.
- 33.Unit Testing: Stop Shipping Garbage, Jonathan Jenkins: The Leader’s Guide to Ending the Cycle of Rework, Delays, and Technical Debt No sample: from the description and table of contents. 2026.
Further reading
Other books in the library on the same ground, not cited above.
- Marionette.js: Testing and Refactoring, David Sulc: Improving Resilience and Maintainability Adding tests to an existing JavaScript application and refactoring parts of it along the way. 2015. Free sample: PDF · EPUB
- Elegant Java, Martin Hock: Hands-on Guide to write Clean Code in Java Fifty-five practices for more readable and safer everyday Java. 2026.
- Python Combat Guide, Carles Mateo: A comprehensive manual about tricks, good practices and coding style for hands on Software Developer Managers and Developers A manual of Python coding style, team practices and unit testing drawn from project work. 2022. Free sample: PDF
- Agile Software In Process, Steven Solomon: A software book with a story An unfinished story of a struggling team adopting Extreme Programming, with messy existing code as part of the plot. 2020. Free sample: PDF · EPUB
- Building Systems Like a Senior Engineer, Aaron Smith Building a card game engine in Python test-first, with refactoring as one step of the cycle. 2025.
- The Professional Junior, Doogal Simpson: Master the Senior Mindset and Write Code for Humans A short handbook for junior developers on writing readable code and pull requests in a team. 2025.