- Podcasts
- The React Show
- Debugging React Apps - The React Show #2
Debugging React Apps - The React Show #2
The React Show
- May 7, 2021
- 38 min
- Season 1, episode 2
About this episodeDebugging can be extremely frustrating and time consuming, but it doesn't have to be. In this Q & A session we will talk about techniques…
Debugging can be extremely frustrating and time consuming, but it doesn't have to be. In this Q & A session we will talk about techniques to more efficiently debug React applications and how to do it without wanting to tear your hair out.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Debugging React Apps - The React Show #2
And welcome to the React Show. I'm your host Thomas with my amazing co-host Austin. We're from React uh Owl Creek Studio. Yeah, we're from Owl Creek Studio. And yeah, we got a great show for you today. We're gonna talk about debugging. Some people, actually a lot of people, don't really like it or they'll hate it sometimes. I kind of like it, but that definitely wasn't always the case. So yeah, we were gonna do a little QA about that. I think it'll be a lot of fun. Last week uh we we didn't have a show because I was MIA finding some uh some new debranks uh out at the low tides. Um and you know, if you if you're not familiar with them, they're these amazing little sea slugs, you know, a few millimeters up to usually a few centimeters. Yeah, super colorful. Um yeah, so that's what I was doing. That's why we couldn't have the show. But you know, we're back. I I do love you know talking about React and all. Um, but sometimes I take a break. Um uh yeah, any anything uh fun that you've been up to lately, Austin?
Or well the topic of today's podcast is definitely uh right on time uh the last few days, just some of the some of the stuff I've been working on uh with some refactoring and implementing some new features that uh I wasn't really sure how to do. Definitely came with a lot of debugging that you helped me work through. And uh I think that spawned the idea of this episode on how to walk through these things. And uh so I look forward to hearing your thoughts on things and having this conversation about debugging.
Yeah, me too. Uh I yeah, I've been um debugging basically for you know close to 20 years now, if not 20 years. Um and I yeah, I've gone through a lot of different stages with it. Um so yeah, I don't know. I don't know if it'll be a useful perspective, but but maybe. So yeah, what what do you got for me?
Well, a lot of the time um I just remember when I first started, um, I would get really frustrated when the code that I was writing wasn't working. And one of the one of the first things one of my mentors said to do is just like slow down and read everything that's coming back to you. What what is the output in the console saying where like tracing back through what you have written and trying to like walk through your own code? Um and like I I taught software development at a school for a while, and one of their main things that they said was try to figure it out on your own for like 30 minutes at most, check out Google if you're like not having any luck, and then then you can like ask someone for help. And I found that anytime that I skip that order and go to helping someone, that within five seconds of talking about the problem, I discover the solution and I end up rubber ducking this person, and it always makes me feel bad.
Uh I I call it um debugging blinders. Um it happens to everyone. Um, you're you're like in that mode, you're trying to figure out what's going on, and you stop really seeing thing things that are there that you know you're like, oh, that's so obvious. How did I not see that? But it just happens to everyone. You get really focused on the debugging session, and you just don't see what's right in front of you. Um, but also I I try not to dwell on that too much. It is a thing that definitely happens, but uh what I've done is try to develop a technique to allow me to keep debugging on my own, regardless of that. Um yeah.
Well, cool. I would like to hear a bit about uh your I guess your routine or your your system for debugging, because I think that having a standard approach to checking off the a list of being like, okay, have I looked at this, have I checked out this is really important for consistency and for honing your craft.
Yeah, um yeah. First I I want to say though, like for everyone that is newer to programming, um, yeah, don't feel bad at all if you spend hours or I I've spent days, literally days, tracking down, you know, a bug. And that you know, ends up seeming to be obvious or whatever. Like, just don't feel bad, don't beat yourself up. It's okay. We all do it, we all go through it. You're not like the only programmer that runs into that. Um, you know, I think that's really important because basically everyone I've worked with that's you know, doing any kind of programming it runs into that situation where they feel bad about not finding a bug faster. Um and I would just say don't, it just isn't a benefit, it doesn't help you to think about that or worry about that. And we've all been through it, it's just how it goes, it's just debugging. Um yeah, so uh if we talk about like actually what I do, um the sort of high level of it is I look, I I try to look at the program in a very methodical way. Um what is my inputs and what are my outputs and you know, my expected outputs and my actual outputs? Um and I always start there. So I try to look in my head and you know, like if I'm working with you on trying to debug something, how do I usually start? I usually start by asking you lots of questions, right? Like, is this supposed to be like this? Is this how this is supposed to be? You know, that's what I'm doing when I'm doing it myself. I'm like, okay, is this really what's supposed to be happening here? Is this really, you know, the way the input should be? And and I will try to look and and narrow down the state as much as I can. And a lot of times I'll hard code the internal state so that I'm dealing with a reproducible situation. Um I guess, yeah, that's that's a big thing, is when you're debugging, you don't want to be debugging a bug that only happens sometimes or that gives you different results every time it happens, or something like that. Like it's really important to start by like pinning it down so that you can actually track things down. And it can be hard, especially if you're doing like multi-threaded programming where you know things are not um determinant, but it's still possible. You just look at your program as you know, I'm gonna edit and modify my program in whatever way I need to to make it so I can go through this process. I'm not afraid to really butcher my program, so to speak, to try to pin it down and get the inputs to be stable and steady and something I can work with and expect to, you know, have an expected result from. So that's really where I start. That's sort of my high level. Um you have any any questions about that?
Well, it sounds like um something like test-driven development is a good habit to start with because it creates that sort of reproducible environment with uh like a certain state. But I mean, I know that especially within React, it's not very easy to uh implement these sorts of things, and especially if you're if you're trying to develop very quickly, um I was going to say um it's hard to always do this, and then after you you've already gotten like half of the work done, and then you run into that bug, and it's like, oh man, like how where do I where do I even begin that's yeah, that's a great that's another thing I was gonna talk about.
A really great question. Um and I actually so you could use a test-driven development approach, which might help you narrow things down. Um, but I I actually haven't found it to really help too much. Um I don't know why. That's a good question. Um, but yeah, so the approach I take is I always start um with the bug itself. Like, you know, it whatever is not happening the way I expect it to be happening, or whatever output I'm not getting that I expect to be getting, I start there and I look at what are my what is the application state or what are my inputs. And so uh I'll be like, okay, like I don't I don't worry about you know anything else in the program. I start there, you know, okay, here is the source of the problem, and here are the inputs to that. Whether it's you know an event that gets triggered or whether it's data that's stored in the state, whatever it is, I log it out, and I'm like, okay, is this the state I'm expecting? And that's often, I think, when I'm debugging things with you, that's the next thing, next questions you start hearing from me is hey, are you expecting this array to be like this or whatever? You know, like that's the that's where I go next.
Well, this this whole concept of like what do you expect? I think uh a large portion of it uh of these bugs could be solved ahead of time by taking the time to walk through a feature implementation with other developers on your team to align on like how is this problem even being solved? That's something that I find is quite lacking, especially at like the current job that we're working on together, is we're like kind of all in our silos working on features independently, and then by the time that a PR rolls out to for you to review, it's like did I even do this in a way that was proper, that like maybe someone else on the team had a more efficient or effective idea on how to do uh this feature implementation. Um and I feel like if that were focused on ahead of time, a lot this whole phase of trying to figure out what the intentions are wouldn't even exist because everyone would be like pre-aligned and the bugs would resolve themselves quicker.
Well, it's like you're saying you have to ask me like what am I expecting, right? And you're looking at this code and it's like, what is this code? Like I don't even know what you're expecting, you know. Yeah, and I feel like that that's a a poor practice that is kind of prevalent in software development because everyone's trying to move fast and get things shipped, you know. We don't take time to like do a pseudocode implementation of what that feature is or like discuss a best practice before going into it. It's more of like uh there's always more of a post-mortem discovery uh process that happens. Yeah, that seems to be standard.
Yeah, um, I I definitely agree with that. Um I I kind of want to move that to a separate topic because I also have a lot to say about that. But if we we could uh what we can do though is talk about like you know, maybe you're just jumping into something that you haven't worked on, so that wouldn't be an expectation, you know, like maybe you're new to it, or or a lot of times I help people debug programs that I've never seen before or never worked on before, and I'm not going to. All I do is help them debug. Um, and I think that could be like um instructive in understanding uh a good process for doing the debugging. So uh yeah, I definitely agree that you know having that really great upfront communication will lead to less bugs and help you understand what to expect and what the code does, certainly. You know, definitely. And that that's definitely a good thing that I think doesn't happen and needs to happen more. Um, but like if we just pretend like either that is happening or isn't happening, but we have a bug and we need to solve it, you know. Um the what what I was talking about before is you know, starting at the source. Well, what do you do then? Like let's say you get some input, and in your case, maybe you're like, I don't even know if that's my expected input. I don't even know if that's what it needs to be. Or maybe you're like, I know that's not what it should be. Um then I do just a simple divide and conquer approach to the program. Um and it can be hard, I think, when you're new to this to really know how to do that. I think it's definitely something where you just need to do it more and have more practice and you'll get better. Um but you can still, even without a lot of practice and experience, uh, start using the technique and it'll help you get better faster, I think. Um so yeah, so I divide and conquer. So like you can think of it like if you have a function and there's a bug at the end of the function, but you don't know where the bug is coming from. You know, we'll just look at a simplified case. I mean, I will jump into the middle of the function and say, is my state at this point what I'm expecting? And if it is, then you're like, okay, great, I know the bug is in the second half. And you can divide that in half, and just that's that's basically what I do. Um now, like you were saying, sometimes you don't know the expected state in the middle of some function. So I will just keep going up the stack until I reach a place that I am at expected state that I can understand. So maybe that means going to the beginning of this function and being like, oh, are all my inputs to this function what I'm expecting? Um and if you're like, I'm not sure, or they're not, I'll I'll go up even higher until I can really actually find some clean place to start from. And then from there, I will often um disable or like comment out a bunch of like things that I don't think are probably going to be relevant. Um so I even maybe a same divide and conquer approach. I will comment out half of the component, or I will have it return null instead of returning output. I will just start like modifying it until I can eliminate the bug and still have clean state. And then I'll work backwards again by re-enabling things, re-enabling functions, you know, modifying inputs to functions. I might um say, I don't know really what this function is supposed to do, but I'm gonna give it this static input, you know. And I know given this input, I should have this output. I don't know how to get it to get that input to start with. Maybe I don't know anything about the program. That's fine. All that matters is we're trying to narrow down where that bug is living.
I like the concept of uh divide and conquer because if you start like all the way at one end, like you could end up like spending a long portion of your journey just working up the stack. But if you start in the middle, you can determine which way you need to go to get there faster. So I like that idea.
Absolutely, that's what I do. Um, and I, you know, and I guess the other topic here is like tools. Um, you know, there are tools like step through tools and stuff, which are great for like more quickly analyzing the state. Um, but I'll be honest, I don't usually use them. I I I will sometimes if I really need to, usually I just log out things, um print line, console.log. Um and I I've even um done this, like I I've written my own operating system and like compiler and an you know integrated interpreter and stuff, and done the same thing, just printing stuff out, and even though I have to like compile it and reload and run everything, like it still works. And the divide and conquer is just the most effective way to do it. You know, I if you know much about computer science algorithms, you'd you know that's probably not surprising, but yeah, that's just a super efficient way to narrow it down. Cool.
Yeah, definitely already learning a bit uh in new paradigms. So I I think it's uh like you said, you if you were to come in to ask me and help me with a bug, uh like asking the questions, then figuring out where to go in and divide and conquer, and then like how to like really isolate the problem. I think these are all really great steps. What do you have? Uh what would your next solution or your next step in the process be?
Yeah, so there's a few other things. I would say, um, you know, this is maybe at this is also at the beginning though, but uh you know, try not to make assumptions. Um I I think at least when I initially was programming and I would find bugs, I I my brain would always be like, well, I know that code works because it worked before, you know, so the bug can't be there. And so I'll be like, it must be over here. And that's almost always the wrong way to do it. Usually the bug is not where you're expecting it to be. Uh so yeah, I I try the whole the whole time during the process, I try not to make assumptions. I use my methods of inputs and outputs and state. I I'm not like, okay, it just it can't be over there. Like that's a really easy thing to fall into that I see a lot of programmers do is yeah, I know this is good. It can't be coming from there. And and the thing is, like, even if that code was perfect with the program how it previously was, there could be some weird interaction that you weren't expecting. And and so it's no longer maybe the bug is there. You know, so it's really important not to make assumptions. And the other part of that helps with your like debugging blindness. Like you'll be able to see weird things in the program better if you just try really hard to not assume every line of code does what it says it does or looks like it does, or function you know, is doing what it says it's doing. Like I'm always checking. Like, oh yeah, that function adds one plus one. Well, does it add one plus one? Am I getting the output I expect? You know, like that's I think that's also a really important thing to keep in mind.
Well, uh, on the topic of assumptions too, also the opposite is like assuming that the problem is somewhere that it isn't, and like looking, like looking in all the wrong places for the problem. Like this is what happened in our most recent example together with uh we have this uh React Hook form custom component that we've made to uh use controlled forms and be able to have all of the the form context and other uh helpers come through it. And I was looking through that file everywhere, like, oh, maybe it's some of these changes that we implemented and was like commenting out stuff with these use effects to see how that was doing, and then it ended up being like right at the base of where the interaction was taking place. The I had left in a line of code that shouldn't have been there, and I was looking in the wrong place the whole time because I assumed incorrectly. Yeah, and waste probably a good 30 minutes.
Yeah, that's exactly what I mean. Like it just it's really hard not to do. I still do it, even though I'm trying really hard not to. Um it's very hard not to do, and it don't beat yourself up, you know, if you end up realizing that's what you did. Um, but if you can remember to try not to make assumptions, it can definitely be really speed up the process.
Yeah, yeah, absolutely. Um yeah, so yeah, I think that's like sort of the the main things that that I do um with with debugging. Um yeah, I I think there's also just a component of experience um that like getting you'll you'll start developing um a knack for like oh I you know I've seen something like this similar before and it was caused by this type of thing, and so maybe I'll go look there to start. Yeah, I won't assume that's where the bug is, but maybe I'll I'll start by looking there and I'll abandon that if it doesn't make sense. Or um that that's uh you know, I think that's something that will happen the more you do it, and so that can also help, you know, uh speed things up. But that's you you can only do that if if you do, like you said, your first uh step at the beginning, which is to try to do it yourself first. Um if you always just get somebody to debug it for you, you're not gonna gain that experience that will make it go better in the long run. Um, but I I definitely think getting experience and debugging, there's just no way around that. You can use the right technique, but having experience will definitely make it go faster. Um yeah, that's just my opinion. I don't know. Maybe there's some people out there that are just you know debugging wizards that don't have that, but for me at least, it took a while.
No, I think that's also a life lesson in there too. You have to like continue to do like repetitious processes that lead to reproducible results in order to advance in any anything. So having having a clearly defined process for debugging is important. Yeah, if you rely on others to help you with your debugging, that's also something that needs to be examined. Like how are like what mistakes are you making when you go to another person and like what kind of things are you like I was saying, I I have this rubber ducking problem where I'll rubber duck with someone for like five to ten seconds and I'm like, oh dang, I've I already found it. So it's like there is a learning that I need to integrate, that I need to do it on my own initially, you know.
Yeah, and I think uh I think part of that too can be um it can be beneficial to take a break and and step back from your debugging as well and from the program. Um I'll I'll do that too. I'll be like, I'm not really finding anything, it's been a half hour. I'm just gonna go do something completely different for 10 minutes and then come back. And oftentimes when I've stepped away and I'm no longer looking at the code, my brain just starts thinking about all their possibilities, you know, like, oh, maybe I didn't check that. Oh, what about that, you know? And then you come back and you're like, oh, I see, okay. Or you you just have new ideas.
Yeah, there's uh actually another um technique I've used. Um so when I I I mean it's a similar technique, but you using a different tool, I suppose. Um so when I was learning to program, I didn't have access to a computer a lot. Um so I did I did a lot of my programming on paper. I would I would sit in like grade school and you know, write down on a piece of paper, I would write out my programs, you know, just like I would on the computer. And I would also run those programs in my head, you know. Um I would like write it out, and then it's like, well, did I is this program any good? And I would literally go through it line by line and I would write out, you know, with my pencil the state of the program line by line. Exactly, you know, this is what I'm expecting. Um and what I found is I would also know it for debugging, I would be like, um, I would run my program finally on the computer, but I didn't have much time. I couldn't sit there and debug it on the computer. So I would print off the code and then you know go to my next class or go home or whatever. And I would go through that code and and do the same thing, writing out the state as I go. And that I don't know what it is, but I found when doing it on paper, I see things I didn't see on the computer. Like on the computer, I'm like rushed, I'm like trying to, okay, I gotta debug this, I figure it out. I just want to figure it out, I want to move on. This thing's really annoying. I I just I want to be done with it, you know. And so I'm like trying all these things. And like you said, you had that case where there was like this obvious to you seeming, you know, thing, line of code that shouldn't have been there. You know, that would happen to me too. But it's like on the computer, I just didn't see it. But I would print it off and I would get to like 10 lines in and be like, oh, well, there's the problem. You know, like it just your brain sees it differently or something, I don't know.
Well, yeah, I mean, it's been proven that when you used handwriting versus typing on a computer, your memory retention about what you're writing is better, and that you like are more connected to what you are writing, and because it instead of it just being a tap on a key, which you can blast out hundreds of these in a minute, you know, writing intentionally drawing out each character makes you evaluate uh much more real time every thing that you're writing.
Yeah. I I think it's actually even more than that though for programming, because what I found was when I'm doing it that way, I'm not able to make assumptions anymore that I was making. So like before, I I would have been like, oh yeah, that function returns this. And when I'm doing it on paper, it's like, what does this return exactly? Like, you know, you you might be logging it out when you're running it, and you're like, oh yeah, whatever, and you just gloss over it. But when you're running it in your head, you're you're like, I need to understand literally everything, or I can't run this program in my head. Like, I need to know what this does. And so you'll be like, oh, I don't I thought this did this, but I'm reading the code now, and that's not what it does, or whatever, you know. Like it just helps you break that like assumptions, you know, thing that can get in the way.
Yeah, you you you should definitely try it. Um even if you don't end up liking to do it long term. Um I think it's a very like good way to change your perspective on programming. Like it just it just forces you to think about it in a way that you don't when you're letting the computer do a lot of the work for you.
Yeah, I'll I'll actually be honest. Um I do, I mean it's not quite the same topic, but I use that technique for refactoring as well, which might sound really weird because you're like, how do you make big changes to code? You know, like you know, with on pencil and paper, but I will I will often print out like 50 sheets of paper, um, you know, or whatever, you know, a big long section of code, and just get out my pencil and I'll start being like, okay, I want to move this block over here and you know, this over here, I'll draw arrows, I'll cross stuff out, I'll write notes. Um, and it's the same effect. Um, it forces you to think about everything. And when you're refactoring, that can be super useful because if you're just hacking it on the computer, like kind of doing the lazy thing where you're like, okay, uh, kind of wanted to do this, I'll move this over here, copy paste this there, cut that there, and you know, just run the program and make sure it's working. You're not really thinking through it. And when you do it on paper, you'll be like, oh, that's a weird name for that variable, or that's a weird interface, or or another thing is I'll be like, wow, this is a lot of writing. Is there a shorter way to write this program? You know, you'll be like, oh, if I rearranged it this way, oh, it's so it's sort of like again another way to get you to have that other perspective.
Well, I think that uh I like the idea of you can kind of see all of the stuff that you've created and how it orchestrates together, whereas typically on your computer screen, like you're looking at a single uh file and then you're like tabbing between files. Uh maybe you have a few of them open at once, like in quadrants or something, but the interconnectivity is still not as apparent as if you actually take out a pen or a pencil and draw on it. And you can be like, oh, like you you kinda I feel like you can get an actual visualization of the flow of what you've written a little bit better.
That is a really great point. Um, yeah, I uh absolutely having being able to see all of it is super useful. And I was gonna say, you know, I don't always like or feel like printing out a lot of paper, so sometimes I'll do it on my like e-ink reader, you know, just just have the code on there, and the one I have you can scribble on it too, just like you would with a pencil. And um it works, but it I was I was actually thinking like, why does that work as well? And I think that's why. Because it's just a tiny screen compared to laying out a bunch of pieces of paper. But yeah, uh, was there any other uh things you wanted to know about debugging?
I think that's my my general uh way, but no, I feel like uh all the stuff you touched upon are really good fundamentals for like creating the debugging mind and how to work through problems. Um definitely going to try to implement all of these before I bug you again with uh another code problem.
So thank you for you're welcome. Honestly, it's not bugging because the truth is I like debugging at this point. Like it's to me, it's like um I really enjoy it. It's like uh a mystery that I need to solve, and it's like so I don't know. Now that I don't get frustrated, I don't really ever get frustrated. I mean, it does, I'll be honest, once in a while I still get frustrated debugging. Usually I don't anymore. It's so it's just this fun, like you know, path, you know, I'm trying to follow to solve a mystery. And yeah, so I I like it. I really like jumping into like yeah, uh like I said earlier, a program that I've never even seen before. I have people do this, they'll be like, I you know, can you help me figure out what's going on? And and you know, be like some server-side thing or whatever in a language I've never worked in, and I'll help them debug it and find the you know the problem. It's just using this technique, you don't even need to know the language, you know, as long as you're working with someone that you know does and can answer those questions, uh you just follow that technique and and you get the answer. And it always feels really good to me at the end. You're just like, Yes, oh, I did it, you know.
Yeah, programming in general, it's uh it can be quite frustrating at times, but then when you finally like are able to merge that PR or you debug that problem, it's like, oh yes, I am like so good. And then everyone that has no clue what programming is, they're like, wait, like what what did you do? I'm like this, like it's the input box. Like I had to like, it's like what? Like, what? It's it's quite funny.
Yeah, uh, you're definitely welcome. And I will, you know, say thank you for for being such a good student on that. Um, I've worked with a lot of people on debugging, and most of them don't take it as well as you. So yeah, that's been fun. Um it's been fun talking about it. Um yeah, uh so uh I guess we'll uh close out the show now. Um this has been the the React show once again. Uh I'm Thomas, and this is my co-host Austin. Uh we're with Owl Creek Studio. Um and the funny thing is, I've left our uh thumbnail uh screen up the whole time, but anyways. Um yeah, so uh it'll just say arg the whole time we're talking.
Yeah, well, now you know I changed it back so you can see our our uh website, all creek.studio, if you wanna if you wanna get in touch with us. Um we definitely love to hear from you. Uh we just uh put out a small ebook that I've been working on. Um so that's fun. You can find that there. Um and yeah, uh we'll we'll probably be with you again uh next week here. Um we we started getting this up on uh on some of the podcast networks, but we have more to go, making progress. Um but yeah, that's uh that's it. I hope everyone has a a good uh weekend here. Um and we'll we'll see you later. Bye.
