4 ms·
The classic "Replace Conditional with Polymorphism" design pattern, which took me way to long to realize that it also applies to Python.
by herge 14y ago
The classic "Replace Conditional with Polymorphism" design pattern, which took me way to long to realize that it also applies to Python.
- ghc 14y agoIndeed it does. I have a large code base at work that uses this pattern for Django models with a bit of metaprogramming to create a template for types of input sensors. It's amazing what this kind of refactor can do when you need to turn a long conditional into something more generally extensible. New sensor types with custom column configurations take an average of 5 lines of code now, instead of about 30 before.
- deltasquared 14y agoYup. I remember that particular realization. Now switch statements in OO languages just look broken to me. Thanks Python!
- jonathanberger 14y agoYes, it's cataloged in Martin Fowler's excellent book Refactoring, Improving the Design of Existing Code in Chapter 9. I'd recommend the book strongly.
- herge 14y agoLink for the lazy: http://www.refactoring.com/catalog/ http://www.refactoring.com/catalog/
- Guillaume86 14y agoI like this kind of refactoring, make you feel like: http://thejoysofcode.com/post/36047317385/when-i-replace-200-lines-of-my-co-workers-code-with-10 http://thejoysofcode.com/post/36047317385/when-i-replace-200...