{ "uuid": [ { "value": "c72430aa-0c6d-4cf8-8a43-812d245ea73d" } ], "langcode": [ { "value": "en" } ], "type": [ { "target_id": "daily_email", "target_type": "node_type", "target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7" } ], "revision_timestamp": [ { "value": "2025-05-11T09:00:01+00:00" } ], "revision_uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "revision_log": [], "status": [ { "value": true } ], "uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "title": [ { "value": "How I work around legacy code" } ], "created": [ { "value": "2025-02-06T00:00:00+00:00" } ], "changed": [ { "value": "2025-05-11T09:00:01+00:00" } ], "promote": [ { "value": false } ], "sticky": [ { "value": false } ], "default_langcode": [ { "value": true } ], "revision_translation_affected": [ { "value": true } ], "path": [ { "alias": "\/daily\/2025\/02\/06\/legacy", "langcode": "en" } ], "body": [ { "value": "\n

A few days ago, I mentioned a method that was over 150 lines long<\/a>.<\/p>\n\n

It had too many responsibilities, nested conditions, no dependency injection and no tests.<\/p>\n\n

Changing it would be risky, so how would I go about it?<\/p>\n\n

Let's assume I have this PHP method that contains the existing logic:<\/p>\n\n

function doSomething() {\n  \/\/ 150 lines of legacy code...\n}\n<\/code><\/pre>\n\n

I can create a new class that's clean and simple, with whatever automated tests and checks I want.<\/p>\n\n

Let's say it has an execute()<\/code> method that returns a boolean value:<\/p>\n\n

class NewService {\n\n  public function execute() {\n    return TRUE;\n  }\n\n}\n<\/code><\/pre>\n\n

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

I want to try and find a seam where I can use the new service and check for a result.<\/p>\n\n

Ideally, this would be as soon as possible within the function:<\/p>\n\n

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

If the new service returns a result, use it and return early, and don't execute the legacy code.<\/p>\n\n

Otherwise, run the original code as it would have been before, falling back to the original logic and result.<\/p>\n\n

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

Here's the thing<\/h2>\n\n

Writing tests for legacy code can be difficult or sometimes impossible.<\/p>\n\n

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

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

Do you do the same thing or do you handle legacy code in a different way?<\/p>\n\n ", "format": "full_html", "processed": "\n

A few days ago, I mentioned a method that was over 150 lines long<\/a>.<\/p>\n\n

It had too many responsibilities, nested conditions, no dependency injection and no tests.<\/p>\n\n

Changing it would be risky, so how would I go about it?<\/p>\n\n

Let's assume I have this PHP method that contains the existing logic:<\/p>\n\n

function doSomething() {\n  \/\/ 150 lines of legacy code...\n}\n<\/code><\/pre>\n\n

I can create a new class that's clean and simple, with whatever automated tests and checks I want.<\/p>\n\n

Let's say it has an execute()<\/code> method that returns a boolean value:<\/p>\n\n

class NewService {\n\n  public function execute() {\n    return TRUE;\n  }\n\n}\n<\/code><\/pre>\n\n

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

I want to try and find a seam where I can use the new service and check for a result.<\/p>\n\n

Ideally, this would be as soon as possible within the function:<\/p>\n\n

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

If the new service returns a result, use it and return early, and don't execute the legacy code.<\/p>\n\n

Otherwise, run the original code as it would have been before, falling back to the original logic and result.<\/p>\n\n

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

Here's the thing<\/h2>\n\n

Writing tests for legacy code can be difficult or sometimes impossible.<\/p>\n\n

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

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

Do you do the same thing or do you handle legacy code in a different way?<\/p>\n\n ", "summary": null } ], "feeds_item": [ { "imported": "1970-01-01T00:33:45+00:00", "guid": null, "hash": "18f69e8494cd5de71961a506175181f4", "target_type": "feeds_feed", "target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76" } ] }