Skip to main content

Posts

Showing posts from March, 2021

Working with Legacy code - How Do I Know That I’m Not Breaking Anything?

How Do I Know That I’m Not Breaking Anything?  Code is a strange sort of building material. Most materials that you can make things from, such as metal, wood, and plastic, fatigue. They break when you use them over time. Code is different. If you leave it alone, it never breaks. Short of the stray cosmic ray flipping a bit on your storage media, the only way it gets a fault is for someone to edit it. Run a machine made of metal over and over again, and it will eventually break. Run the same code over and over again, and, well, it will just run over and over again. The  Software Crafters community The  Legacy Code Rocks community The  WeDoTDD community

Working Effectively with Legacy Code chap 7 - It takes Forever to make a change - Understanding, Lag Time and Dependencies

Breaking dependencies Build Dependencies aha The Dependency Inversion Principle Now we have a package, OpportunityProcessing , that really has no dependencies on the database implementation. Whatever tests we write and place in the package should compile quickly, and the package itself doesn’t have to recompile when we change code in the database implementation classes. The Dependency Inversion Principle When your code depends on an interface, that dependency is usually very  minor and unobtrusive. Your code doesn’t have to change unless the interface changes, and interfaces typically change far less often than the code  behind them. When you have an interface, you can edit classes that implement that interface or add new classes that implement the interface, all without impacting code that uses the interface. For this reason, it is better to depend on interfaces or abstract classes than  it is to depend on concrete classes. When you depend on less volatile things,  you minimize the ch

Working with Legacy code - Sprout Method, Sprout Class and Wrap Method, Wrap Class

 Wrap Method Adding behavior to existing methods is easy to do, but often it isn’t the right thing to do. When you first create a method, it usually does just one thing for a client. Any additional code that you add later is sort of suspicious. Advantages and Disadvantages Wrap Method is a good way of getting new, tested functionality into an application when we can’t easily write tests for the calling code. Sprout Method and Sprout Class add code to existing methods and make them longer by at least one line, but Wrap Method does not increase the size of existing methods. Another advantage of Wrap Method is that it explicitly makes the new functionality independent of existing functionality. When you wrap, you are not intertwining code for one purpose with code for another. The primary disadvantage of Wrap Method is that it can lead to poor names. In the previous example, we renamed the pay method dispatchPay() just because we needed a different name for code in the original method. If

Working with Legacy code - Fake Objects and Seam model

Fake Objects Support Real Tests Sometimes when people see the use of fake objects, they say, “That’s not really testing.” After all, this test doesn’t show us what really gets displayed on the real screen. Suppose that some part of the cash register display software isn’t working properly; this test would never show it. Well, that’s true, but that doesn’t mean that this isn’t a real test. Even if we could devise a test that really showed us exactly which pixels were set on a real cash register display, does that mean that the software would work with all hardware? No, it doesn’t—but that doesn’t mean that that isn’t a test, either. When we write tests, we have to divide and conquer. This test tells us how Sale objects affect displays, that’s all. But that isn’t trivial. If we discover a bug, running this test might help us see that the problem isn’t in Sale. If we can use information like that to help us localize errors, we can save an incredible amount of time. When we write tests fo

PHP array_merge vs +, a case study on recursive array as tree

    private function extractSubCategories($categories)     {         $result = [];         foreach ($categories as $key => $node) {             if (empty($node['sub_categories'])) {                 $result[$node['id']] = $node;             } else {                 $tmp = $node['sub_categories'];                 unset($node['sub_categories']);                 $result[$node['id']] = $node;                 $child = $this->extractSubCategories($tmp);                 $result = $result + $child;             }         }         return $result;     } More detail ... Damn it! Simple change array_merge to '+' // Second way         // foreach ($debug as $key => $node) {         //     if (empty($node['sub_categories'])) {         //         $result[$node['id']] = $node;         //     } else {         //         $secondStep = $secondStep + $node['sub_categories'];         //     }         // }         // // dd($resul

Mysql search multiple dot REGEXP

regexp . . dot mysql .\..*\.. SELECT * FROM `collection` WHERE `submodel` REGEXP '.*\\..*\\..*' LIMIT 50 https://smallbusiness.chron.com/record-payable-isnt-expense-until-future-period-40042.html Clayton Steam Generator https://www.history.com/news/how-stereotypes-of-the-irish-evolved-from-criminals-to-cops https://stackoverflow.com/questions/52712589/shopify-cli-theme-deploy-changed-file-only https://community.shopify.com/c/Shopify-Design/How-to-push-up-singe-file-with-ThemeKit/td-p/452644