Code tip: clean up after yourself (and a seltzer review)
Poppin off is a series of short code tips, tricks, patterns, and refactors for software developers. Oh and also seltzer reviews. Clean up after yourself! It this video we cover two different methods to clean up after yourself. The first is returning undo functions and the second is maintaining a cleanup array.
We also do a Lemon Spindrift review, our first seltzer. Send me a seltzer or hipster soda!
Everything I said
Welcome to Poppin' Off. This is a series where I'm gonna pop off about code-specific topics. That could be refactorings, best practices, patterns, whatever it may be. We'll keep it nice and short, scoped down to a single idea, and all the while, we're gonna be drinking something refreshing. Today, we're popping open a Spindrift lemon sparkling water.
Now, normally, flavored sparkling waters have the mere suggestion of a flavor. It's as if you're drinking a regular water and someone in a room next door shouts a flavor. That's normal sparkling water. However, Spindrift actually has a flavor. No sugars, three calories. We're gonna be drinking the Spindrift. Let's talk about cleaning up after yourself in code.
This is a Laravel command, but before you leave, and before you leave a comment that tells me how much PHP sucks, I know that it sucks. Everybody has told me that. This applies to every framework, every language. This applies to your life. You've gotta clean up after yourself. Come on, guys. So, here's what we're working with. We're gonna start a process and touch a file, and then we're gonna write some stuff out, and then we're gonna run in an infinite loop until we interrupt the command, at which point it will exit.
So, if we were to look at this, we can just run app run, and we'll see starting ngrok, touching a file, waiting, and now we're in this infinite loop, and stopping and exiting. This is the command that we're working with. However, we don't ever, uh, delete this file that we touched, and we don't ever make sure that this process that we started is terminated.
It will likely be terminated when the parent process is terminated, but we should go ahead and clean up after ourselves and make sure that everything is spic and span. So, there are two ways that we can do this. The first one is to return a callback, and the second one is to register an undo stack. Let's start by returning a callback. To do that, we're gonna go into each function and return a callback that undoes itself.
So, we're gonna assign this process a variable, and then we're gonna return a function that uses that process, and we can say, if process running, then process signal, and we're gonna send the sig int, so we're going to interrupt. And we'll say, uh, in fact, let's say down here, this line, stopping ngrok.
And we can return that, and now this start process returns a stop process. God, that just feels so clean, doesn't it? So then, down here, we can say stop process, and then let's go do the same thing in touch file. So we'll come down here, and we will return a function that says at unlink.
And so if the file is gone, we actually don't care, uh, but we do wanna make sure that it is gone. So we'll say unlink that file, and we'll just put, uh, this line, unlinking file. And that should be good enough. And now we have a, uh, destroy file. Could say delete, but destroy sounds a lot more intense, so I'm gonna go with destroy.
And down here. So now, what we've done is each process returns its own undo function, such that if we were to run this in here, you should see it's starting, it's running, running, running, and we interrupt it. Boom. Stopping ngrok, unlinking the file, and exiting. The second way that we can do this is with like a, uh, like a callback stack.
So let's say protected, um, array cleanup. Uh, yeah, we'll just call it cleanup callbacks to be extra explicit here. And I kinda like this because you could have potentially, a, a process could potentially add many callbacks to the stack, which, you know, I kinda like. You could, of course, wrap all that up into a single function, but I kinda like this.
So instead of returning, we are going to just push this onto the stack. So this callback, this cleanup callbacks equals this new function. And we can come down here. Instead of returning that guy, we'll do that. And then we just need to write, uh, we just need to write a cleanup function. I'll just put it up here for giggles. Public function cleanup.
You could probably just inline this, but we're already here, so for each this cleanup callbacks as CB, and then we can just say CB. Now all you have to do is say this cleanup, and we should get the exact same output that we were getting before. So we see starting, touching, waiting, and stopping, unlinking, exiting.
Here's what I like about this. Regardless of which implementation you choose, whether that's returning the callback function or pushing onto a callback stack, it keeps the action and the reversal of the action very close together. So one method is responsible for declaring how to instantiate and how to tear down, even if the tear down comes in a different place and much, much later.
I like this, that it keeps it all co-located together. Makes me feel good. Also makes me feel nice that I'm sure we've cleaned up after ourselves at the end. We're not leaving processes running or files hanging around. Now, normally, I'm gonna rank, I'm gonna rank what we're drinking. This is the first one, so we're gonna rank Spindrift lemon at the top.
This is the number one right now. There will be a lot more, so I don't know if it's gonna hang on to the crown, but for now, way to go, Spindrift lemon being number one. Please stay tuned for more Poppin' Off. Until the next time, see ya.