---
title: "Code tip: Don't undo what you haven't done"
url: "https://aaronfrancis.com/videos/code-tip-dont-undo-what-you-havent-done"
path: "/videos/code-tip-dont-undo-what-you-havent-done"
type: video
author: Aaron Francis
published: 2024-08-02
topic: "Laravel & PHP"
keywords: [dont undo pattern, poppin off series, cache pattern, undo function, laravel, refactoring, code patterns, toggle pattern, mineragua]
youtube_url: "https://www.youtube.com/watch?v=xGxJyDgU8NQ"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

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

## Chapters

- Welcome to Poppin Off Series
- Seltzer Overview
- Don't Undo What You Haven't Done
- Solution 1 - Cache
- Other Implementations in Laravel
- Solution 2 - Return Undo Function
- Seltzer Ranking

## Transcript

**Aaron Francis:** 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...

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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 ...

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

---

## Related on aaronfrancis.com

- [Code tip: clean up after yourself (and a seltzer review)](/videos/code-tip-clean-up-after-yourself-and-a-seltzer-review) *(Video · 2024-07-26 · shared: refactoring, poppin off series, code patterns)*
- [About](/about) *(Page · 2026-11-25 · shared: laravel)*
- [Now](/now) *(Page · 2026-11-25 · shared: laravel)*
- [Biggest Week In Laravel History](/podcast/biggest-week-in-laravel-history-cfbcddfa) *(Podcast · 2026-11-24 · shared: laravel)*
- [Oops!...Ian Did It Again](/podcast/oopsian-did-it-again-8cf7ed7d) *(Podcast · 2026-11-17 · shared: laravel)*

## Relevant Resources from Aaron Francis

- **[Solo](https://soloterm.com)** (overview on site: [/software](/software)): All-in-one agentic development platform and terminal workspace built by Aaron Francis for orchestrating processes and AI coding agents.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `dont undo pattern` ([D-E](/llms/keywords/d-e.txt)), `poppin off series` ([O-P](/llms/keywords/o-p.txt)), `cache pattern` ([C](/llms/keywords/c.txt)), `undo function` ([T-Z](/llms/keywords/t-z.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
