The forgotten reason refactoring code into small units is still good
People debate... like... a lot... about how far to break down block of code into smaller units. Recently I even tried to start an argument with someone who was upset at methods that only have 1 line in them. The biggest argument for-or-against refactoring is usually [for small units] "but mah test suite" and [for larger units] "this is my logic and i want it this way".
I am actually in the tiny unit camp these days but not because of testing. Feels like most people have forgotten what the original and best reason for having small units was... being able to modify something very easily with a sharp focus. Here is an example of why 1 line methods are good. This is a jQuery based JS class I am working on for a UI toolkit, as you do when you are a web developer who is avoiding real work. The constructor of this class is given an HTML template and these methods are able to locate a label and set the text within it.
class TextField { // ... findLabel() { return this.element.find('label'); }; setLabel(content) { this.label.text(content); return this; }; // ... };
Here is the kicker. Right now if you send this method some HTML you will see the literal HTML as text within the label. Adding a icon from MaterialDesignIcons will display the statement <i class="mdi mdi-user"></i> instead of rendering the icon glpyh.
In my godlike infinite wisdom I have forever limited you to only ever putting plain text there. I did this to be lazy regarding HTML injection issues and also, I personally do not like form labels that are super busy. So I wrote this code to purposely limit your creativity for the sake of my sensibilities.
Or... did I?
So lets say you do want HTML in your labels. Probably to add things like those icons. A super reasonable request that a much younger version of me would refuse to accept a pull request for because he thought that idea was dumb. To be fair Old Me still probably would reject the pull too, but at least he would send you a link to this blog post after.
The trend of people hating polymorphism is still really strong and weird. It makes people forget the power they have. My original code did not constrain. On the contrary, it provided everything needed to do what is wanted without bothering me or knowing much about the rest of the class's implementation. 
class OurTextField extends TextField { setLabel(content) { this.label.html(content); return this; }; };
Now you have a fully working class with all my features but gave it the ability to have fancy labels. No pull request needed. No arguments had. You are 100% allowed to have local code that does what you need without living in packagist/npm/whatever, and without submitting an unreadable AI written pull request upstream.
You can just do this, its wild!
And yeah this is why I loath the `final` keyword in PHP, it prevents extending the class. It disallows it. It is very, very arrogant. A trait that is fairly simple to refactor out of code.
OPSAT.net
SSL Monitoring | Email Alerts | JSON API