Be Patient With Abstractions
There is a file in the Livewire codebase FULL of duplication. There certainly is an abstraction just begging to be made. Why can't I find it? What do I do about that?
Transcript:
Okay, so I'm writing a test a JavaScript test in Livewire and I am about to use a testing utility that I've created. So I have this utils dot J's file inside my testing folder for JavaScript and it's where I have all of my testing helpers and they're all in charge of the same thing. They're in charge of mounting Livewire.
Making Live Wire breathing Live Wire into existence in JavaScript and usually you pass in there for a component. So I think the basic function I have is called Mount and you pass in HTML the HTML of the component and that's it. And then once you've done that you can say Mount and then you pass the HTML now you can do stuff with it.
You can you know document that query selector get a button that has wire click on it click and then you can assert that, you know some payload was sent. So there's a bunch of these Mount helpers started out as one called Mount and I tried to make it do too many things in a star to get gross. So I just duplicated it and called the next one mount with data may be so I have a bunch of them and there's mount mount as root Mount is Route and return mount with a vent mount in return.
Mount and error mount with data there's tons of them and they all have the almost the exact same structure slightly different parameters, but the basic structure inside of them and they all just do something slightly different. So I know that there is an abstraction waiting here someday, it will occur to me, but it is not it hasn't occurred to me yet and I'm just being patient with it.
So I'm speaking this out to you. As a reminder as a thought piece, whatever that sometimes you just have to be patient with abstractions. So here I want to write a test and I realized I need a new one. I could try to retrofit and another one but really I need a new one and it's going to be a very specific one.
And I know and that's a little bit of pain like I'm experiencing pain because I'm copying pasting a bunch of code. And I've had to change things that I've missed and changing another ones and I've experienced issues and pain because of that. So this is not ideal and I'm doing it and I recognize that I know it's not ideal.
It's not good, but it's an ideal on ideal scenario because this is all scope to one file. It's one big file and they're just. A ton of basically the same method with a different name different parameters in slightly different implementation, but they're all in one file. So it's not like I have it's not spread across different files this kind of goes back to the single file principal like my tolerance for nastiness is a little bit higher when it's all scope to one file.
And in this case it is but anyway, I went to add this thing and I just kind of realized that it's painful and I thought for a second. Is there any abstraction emerging yet? And no it's not. But at some point it will maybe it's just me being a little bit lazy and just wanting to copy and paste instead of roll up my sleeves and really figure something out that works for everything.
But really it's not it's not causing big problems in my app and the abstraction hasn't prevented it presented itself. So I'm just going to be patient with it. Being patient with abstractions can one first thought was being patient with abstractions can be a little bit more dangerous when there's no trigger set up and by trigger, I mean something that's going to remind you that you need an abstraction in this case.
My trigger is having to add a new function. That's a pain Point triggers me to think about abstractions if that didn't exist. Then I could let bad abstractions or unobstructed code lie dormant my next thought with that was well if I construct a code is lying dormant and being harmless and there's no trigger that makes you feel pain then maybe it's fine.
Maybe you don't need the abstraction because abstractions are meant to serve us. We're not there to serve abstractions. So anyway, there is an abstraction waiting here. That would serve me. I know it. I know it would make my tests more flexible. I know I know there's something there's some implementation.
That allows the developer the flexibility to do what they want to do without needing all these different methods, but for now, I'm going to copy and paste one of these methods and add yet another method called Mount and return listener or something like that, whatever. I'm about to do it, and that's all I wanted to tell you.
Sometimes you have to be patient with abstractions. Don't rush it. Hopefully it presents itself to you, but don't force it if it's not ready yet. So, thanks.