August 202408:0210.7k views

Code tip: Don't undo what you haven't done

About

Poppin off is a series of short code tips, tricks, patterns, and refactors for software developers. Oh and also seltzer reviews. Don't undo what you haven't done! Turn something off, do a thing, turn it back on. Seems so simple, but you might be undoing something that you never did in the first place.

We also do a review of Mineragua. Send me a seltzer or hipster soda!

Transcript

Everything I said

00:06

Welcome back to Poppin' Off, where we pop off about one specific code topic, whether that's a refactoring, a best practice, a pattern, anything that we can make in maybe less than 10 minutes, maybe less than five, while drinking something delicious. Today, we're having a Miniragua. Pretty good pronunciation, honestly. Miniragua is...

00:27

I can't keep doing that. Miniragua is a plain sparkling water that has its own jingle. Steve, can we hit them with the jingle? Miniragua Sparkling Water. It's time for new adventures. All right. The, the water is good. That much I can say. The water is great. It is extremely, extremely bubbly, so if you're not into that, uh, you're not gonna like this.

00:50

I happen to really like it. I also like that it comes in glass. Miniragua, very good. Now, what are we popping off about today? Today, I wanna pop off about not undoing things that you haven't actually done. You can undo things that you have done, but don't undo things that you haven't done. Here's the command that we're working with.

01:10

This is just a Laravel command. We're gonna run super_secret and not_secret. And in super_secret, we're gonna log out, you shouldn't see this. And in not_secret, it's okay to see this. If we run that, you'll see, uh-oh, you shouldn't see this, and it's okay to see this. So let's first work on hiding that part. And to do that, we are just gonna switch this, uh, logging, logging enabled.

01:33

We're gonna switch that to false, and then after we run our super_secret, we're gonna turn it back on. So if you run that now, you only see the log that you're supposed to see. Hey, this is fine. Uh, this is not, this is not good enough, in my opinion. I don't want to be doing this. This is some sort of implementation detail that I don't super care about. So one thing that you will often see is you'll see something like this.

01:56

This without logging, and then what they do is they allow you to pass a callback in. And so we'll run super_secret within that context. And then we need to take these guys and come down here to public function withoutLogging, and it's going to accept a callback. And then in here, we are just going to, let's say, try running the callback.

02:20

So we'll just say callback. And then we're gonna not catch. We're just gonna say finally, and we're gonna turn the logging back on. So no catch. So if there is an error, we turn the logging back on, and then we throw the exception. So now if we run this again, hey, we're in a pretty good spot. It's okay to see this.

02:42

However, we have just made a tragic mistake. Tragic mistake. If we were to do this, and then imagine you're off in some other... You're in some other class that is not right here, and you were to do this again, 'cause you're like, "Oh man, I'm in this other class and this is really secret. I need to turn off logging." Watch this. Oh, no, what have we done? Uh-oh. You shouldn't see this.

03:04

What we have done is we've undone something that we never did in the first place. Somebody else turned off logging, and then we turned off logging and turned it back on before this guy could turn back on their logging. So they did something, we undid it, and now we're hosed. The first time we enter this closure, we set it to false, and then immediately after that, we set it to false again, and then when we exit this closure, we set it to true, even though we shouldn't really reset it until we exit the second closure.

03:33

So the way around this is we can do this. Watch this. So if we do, um, instead of s- explicitly setting it to true at the end, we're gonna cache. We're gonna say loggingEnabled equals this.loggingEnabled. So we're gonna say, "Hey, I'm just gonna respect whatever it is right now. I'm gonna explicitly turn it off, but when it comes time to turn it back on, I'm actually just gonna turn it back to whatever it was," because we don't wanna undo what we haven't done, meaning we can now run this and see, it

04:04

is okay to see this. So we have fixed our problem. This is a pattern that you'll see in many frameworks. I'm gonna show you some spots in Laravel that does this. There's also another way to handle this, and that's by returning an undo function, which is kind of cool, and we'll do that after I show you these two spots. If we were to look at the user model, let's just say user without, and we'll do withoutBroadcasting.

04:26

So if we look at that, you'll see, hey, this looks familiar. It accepts a callable, and then it caches the value for isBroadcasting, sets it ex- explicitly to false, and then it has the try finally static isBroadcasting equals isBroadcasting. I like that. In fact, I think we didn't return... Yeah, let's do that. We need to be good citizens, so we're gonna return the value of that callback in case anyone's relying on it, which we're not here, but other places you might be.

04:53

Another one you'll see is withoutEvents. Um, so if you look at withoutEvent- events, it's very similar. If there was a dispatcher, they set it to a null dispatcher, and then at the end, they set it back to the cached dispatcher. So this is, uh, a good indication to me that if a big framework like Laravel is doing it, it's a pretty good idea.

05:14

So I told you there's another way to do this. If you don't like this nesting s- this, this callable stuff, you can do it like this. Let's first write what we wanna see, and then we'll figure out how to make it work. So withoutLogging, and then we can say, uh, undo equals withoutLogging, and then we'll just come down here and say Undo.

05:35

After we've done all our super secret stuff, we can say, "Hey, I want to undo and put it back to whatever it was." Now, to modify this, we need to say callback equals null, so it is a nullable callback. And then instead of, um, if isNull- Callback, then return a function that is, um ...

06:00

Actually, let's do ... Why don't we do this? Let's just share this. Let's just say undo equals a function, and the ... We need to pull in use logging enabled, and then we're gonna take this guy and yonk, and we're gonna put that up there. And now we can replace this with undo. And if there's no callable or callback, we can simply return, return that for them to hopefully clean up after themselves, which I have a video on.

06:28

Make sure you clean up after yourself. So now we should be okay up here, and app run. It is okay to see this. And then if often some other distant class you're doing something like this, um, you are doing and undoing over there, we'll re- need to rename these. Um, but if you're doing and undoing in some other distant class, you're still respecting who has done what, and you're not undoing someone else's action, so you're still in a good spot.

06:56

This is a very common pattern, and it normally expresses itself like this. Turn something off, do something, turn it back on, or turn something on, do something, turn it back off. Just remember when you're finally done to turn it back to what it was before, not the opposite of what you explicitly set it to. So cache that initial state and then reset it at the end.

07:16

Now, for the thing you have been waiting for. Where does the Miniragua rank on our list of sparkling water/sodas? Um, so far we've only done Spindrift Lemon and Miniragua, so I guess it depends. I guess it depends, which is not very helpful. If you're looking for something flavored, go with the Spindrift Lemon. If you're looking for something not flavored, the Miniragua is great.

07:40

This is a top-tier sparkling water. If you have a sparkling water that you like, please send it to me, not via email, via the real mail. I'll put a physical address in the description. Send me your sparkling waters. I will taste it. I will rank it. I will pop off about it until the next time. See ya.

Laravel & PHP

More videos on Laravel & PHP

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues