10 hours ago
JSn1nj4 pushed to feature/datetime-tools at JSn1nj4/frontend-stuff
Categories: Good Practices
A lot of programming involves abstractions—a lot of abstractions: classes, traits, wrapper functions and components...
Hi, my name is Elliot, and I'm a recovering abstractaholic (is that a thing?)
Like some other devs, I've gotten into the habit of abstracting early, just because I get an idea in my head of how I think an ideal abstraction for a thing I'm making will work.
This can lead to some issues:
A good solution I've heard about lately: Write Everything Twice (WET). It's fine if the same thing has been written only a couple of times. After the second or third time, it becomes much clearer what a good, shared abstraction might be. The implementation becomes better, and issues that may have been present the first time had more chances to be worked out.
In languages that object-oriented programming is the norm (like PHP), defaulting to classes is easy, and so is extending them.
Due to this, it's also easy to create a huge inheritance "tree" of deeply-nested classes. This is something that can draw complaints from both OOP and Functional programming crowds (though the latter tends to be fully against OOP-like programming anyway).
Shared behaviors are helpful in a lot of ways, but more nesting can just produce more clutter and unnecessarily tight coupling. Luckily, there are ways to trim this down to a more maintainable degree:
This has some shared elements and pitfalls of the other 2. When abstracting, you're usually trying to encapsulate common/shared behaviors. Sometimes that can go too far.
For example, I'm sure all front-end devs have written wrapper components in a framework like React or Vue. In those, it's common to write wrapper components that add extra behaviors or even just some common defaults.
In doing this, every now and then I've found myself making my wrappers too generalized. Doing this can get you very close to reinventing the thing you're wrapping—a vanilla HTML element or even a third-party UI library component.
I'm not really sure what antidotes exist for this. But I remember the last time I did this, I undid some of my wrapper components and just wrote the HTML—basically a reset. The front-end component I was writing had the logic it needed anyway, so it was fine letting it worry about what it needed to render.
When tempted to reach for an abstraction, just wait a little longer to see if something better comes along. You'll end up having to refactor something later anyway, but it'll be simpler to refactor basic code patterns that have repeated a couple of times rather than abstractions that were incorrect—especially if those bad abstractions have become pervasive in a short time.
Thanks for reading!
10 hours ago
JSn1nj4 pushed to feature/datetime-tools at JSn1nj4/frontend-stuff
14 hours ago
JSn1nj4 pushed to feature/datetime-tools at JSn1nj4/frontend-stuff
15 hours ago
JSn1nj4 pushed to feature/datetime-tools at JSn1nj4/frontend-stuff
15 hours ago
JSn1nj4 pushed to feature/datetime-tools at JSn1nj4/frontend-stuff
16 hours ago
JSn1nj4 pushed to feature/datetime-tools at JSn1nj4/frontend-stuff
Please confirm whether you would like to allow tracking cookies on this website, in accordance with its privacy policy.