"value":"\n <p>A few days ago, I mentioned <a href=\"https:\/\/www.oliverdavies.uk\/daily\/2025\/02\/03\/testable\">a method that was over 150 lines long<\/a>.<\/p>\n\n<p>It had too many responsibilities, nested conditions, no dependency injection and no tests.<\/p>\n\n<p>Changing it would be risky, so how would I go about it?<\/p>\n\n<p>Let's assume I have this PHP method that contains the existing logic:<\/p>\n\n<pre><code class=\"php\">function doSomething() {\n \/\/ 150 lines of legacy code...\n}\n<\/code><\/pre>\n\n<p>I can create a new class that's clean and simple, with whatever automated tests and checks I want.<\/p>\n\n<p>Let's say it has an <code>execute()<\/code> method that returns a boolean value:<\/p>\n\n<pre><code class=\"php\">class NewService {\n\n public function execute() {\n return TRUE;\n }\n\n}\n<\/code><\/pre>\n\n<p>Because it's been tested in isolation, we can be confident it works as needed, so I can use it in the legacy function.<\/p>\n\n<p>I want to try and find a seam where I can use the new service and check for a result.<\/p>\n\n<p>Ideally, this would be as soon as possible within the function:<\/p>\n\n<pre><code class=\"php\">function doSomething() {\n $newResult = $this->newService->execute();\n\n if ($newResult) {\n return $newResult;\n }\n\n \/\/ 150 lines of legacy code...\n}\n<\/code><\/pre>\n\n<p>If the new service returns a result, use it and return early, and don't execute the legacy code.<\/p>\n\n<p>Otherwise, run the original code as it would have been before, falling back to the original logic and result.<\/p>\n\n<p>Over time, more logic can be migrated into the new service until the legacy code is no longer used and can be removed.<\/p>\n\n<h2 id=\"here%27s-the-thing\">Here's the thing<\/h2>\n\n<p>Writing tests for legacy code can be difficult or sometimes impossible.<\/p>\n\n<p>This approach means new code can be written using tests and test-driven development, dependency injection and whatever else you want without being limited by the existing code.<\/p>\n\n<p>You can incrementally migrate to the new approach and refactor out the old code, making it less risky than an all or nothing approach.<\/p>\n\n<p>Do you do the same thing or do you handle legacy code in a different way?<\/p>\n\n ",
"format":"full_html",
"processed":"\n <p>A few days ago, I mentioned <a href=\"https:\/\/www.oliverdavies.uk\/daily\/2025\/02\/03\/testable\">a method that was over 150 lines long<\/a>.<\/p>\n\n<p>It had too many responsibilities, nested conditions, no dependency injection and no tests.<\/p>\n\n<p>Changing it would be risky, so how would I go about it?<\/p>\n\n<p>Let's assume I have this PHP method that contains the existing logic:<\/p>\n\n<pre><code class=\"php\">function doSomething() {\n \/\/ 150 lines of legacy code...\n}\n<\/code><\/pre>\n\n<p>I can create a new class that's clean and simple, with whatever automated tests and checks I want.<\/p>\n\n<p>Let's say it has an <code>execute()<\/code> method that returns a boolean value:<\/p>\n\n<pre><code class=\"php\">class NewService {\n\n public function execute() {\n return TRUE;\n }\n\n}\n<\/code><\/pre>\n\n<p>Because it's been tested in isolation, we can be confident it works as needed, so I can use it in the legacy function.<\/p>\n\n<p>I want to try and find a seam where I can use the new service and check for a result.<\/p>\n\n<p>Ideally, this would be as soon as possible within the function:<\/p>\n\n<pre><code class=\"php\">function doSomething() {\n $newResult = $this->newService->execute();\n\n if ($newResult) {\n return $newResult;\n }\n\n \/\/ 150 lines of legacy code...\n}\n<\/code><\/pre>\n\n<p>If the new service returns a result, use it and return early, and don't execute the legacy code.<\/p>\n\n<p>Otherwise, run the original code as it would have been before, falling back to the original logic and result.<\/p>\n\n<p>Over time, more logic can be migrated into the new service until the legacy code is no longer used and can be removed.<\/p>\n\n<h2 id=\"here%27s-the-thing\">Here's the thing<\/h2>\n\n<p>Writing tests for legacy code can be difficult or sometimes impossible.<\/p>\n\n<p>This approach means new code can be written using tests and test-driven development, dependency injection and whatever else you want without being limited by the existing code.<\/p>\n\n<p>You can incrementally migrate to the new approach and refactor out the old code, making it less risky than an all or nothing approach.<\/p>\n\n<p>Do you do the same thing or do you handle legacy code in a different way?<\/p>\n\n ",