Yesterday, We had a company Xmas party. Before dinner at a restaurant, we went to the escape room event. If you are unfamiliar with escape rooms, they're interactive puzzle experiences where you and your teammates are "locked" in a themed room and must solve clues to escape within a time limit. It's a fun challenge that puts you in a detective role. It tests your creativity and problem-solving skills. In many cases, debugging is like being the detective in a crime movie where you are also the murderer. But since there is a lot of existing code, you need to know how to debug it when fixing a bug. Here are 10 tips on how to debug and resolve any mystery in your code. 1. Identify a list of reproduction steps ("Interrogate the witnesses" approach)Before you can fix the issue, you need to know two things. First, you need to know how to reproduce it. In some past projects I’ve worked on, the bug tickets we got contained a list of steps on how to reproduce it. Or a video containing the steps. Obviously, not every project will have this. But it speeds up bug-solving tremendously. Second, you need to understand what the expected behavior is. Clarify and confirm this before you start fixing the issue. So you don’t fix the wrong thing. 2. Write a test that reproduces a bugHow to make debugging less time-consuming? When you have a scenario where a bug occurs, you can write a test for that. So you don't have to go through the UI every time to get to the code that contains the bug. I call this the “Peace of Mind Method”:
With this technique, the same bug won't occur ever again. You get one more test. And one less thing to worry about. 3. Use debugging tools effectivelyUsing debugging tools in 2024/2025 goes beyond just stepping through the code. It means:
Just to name a few. Debugging tools got powerful. Learn how to use them. However, debugging only covers the current state of code. For more information, you need… 4. Browse through code historyDebugging, although powerful, will only help you understand the current state of the code. Another useful method for identifying the bug is going through the Git history and checking how the code has evolved. If the bug is fairly recent, you can inspect only the commits between now and the last version that didn’t have a bug to identify the issue. 5. Use the divide and conquer approachThe divide and conquer approach is a method for identifying the root cause of a bug by breaking down the problem into smaller parts. And narrowing the scope of the investigation step by step. This technique is useful when you can’t exactly debug the whole execution flow line-by-line. To perform it, you do a binary search for a bug:
Using this approach, you can quickly identify the section of the code where the issue could be. 6. Use AI to help youThis is pretty self-explanatory. But for the sake of completeness, I’ll mention it. Before the AI era, you usually pasted the error message in Google and searched in Stack Overflow for an answer. Nowadays, you can put the code snippet or an error in the ChatGPT and ask him to identify the potential issue. Based on my experience, this can be a hit-and-miss. It will pinpoint the obvious issue you have missed while investigating. But it can struggle if the code is more complex. Either way, it's worth trying. Speaking of your tries…. 7. Keep a list of what you have triedIf you keep the list of approaches you’ve tried, you can avoid repeating the same steps. But expect different results. Also, when you ask a team member for help, you can tell him what you tried. So that you skip those suggestions. 8. Don’t assume the error is in 3rd party libraryIf you don’t fully know the 3rd party library your code is interacting with, you might assume that the bug is in the external package. Unless you work with Xamarin.Forms, 97% of the time 3rd party code is fine. That is, if you use only popular and well-tested packages. But you do that, right? 9. Improve observability (Collect evidence from the crime scene)In cases when it is hard or impossible to reproduce the bug, what you can do is improve observability. Add more logging to your code to collect helpful information from the crime scene: your production environment. 10. Take a breakOnce you get to the point where you:
Stop. Take a break. Go get some water/coffee/tea, and step away from a computer for a few minutes. Your mind will subconsciously continue to work on the solution. But you will get the chance to clear your mind. And calm yourself. That’s why one of my favorite quotes about debugging is: “Sleep is the best debugger.” And remember: Debugging is not just a skill. It’s a superpower. It’s a way to fix what’s wrong in the code written by a human. Or AI. Improve your debugging skills. And you'll improve your productivity. Have a nice weekend.
|
Weekly newsletter packed with code-improving tips, tools, and strategies to become a better .NET developer.
Lately, I’ve been paying more attention to what’s happening in the AI space. Maybe because of all the hype that surrounds it. Maybe because of the anxiety of whether AI will take my software development job. That's why I’ve decided to spend some spare time during the Xmas holiday to explore the state of AI software development tools. This email combines: My 1+ years of experience using a paid version of GitHub Copilot. 5+ hours of YouTube videos I’ve consumed in the last 2 weeks. So, let’s...
2 weeks ago, .NET 9 was released. If you haven’t had time to read the official release docs, don’t worry. I spent 1 hour investigating what's new in .NET 9. So you don't have to. Here are the top 10 improvements for C#, ASP.NET Core, and EF Core. 1. LINQ Index LINQ has always been an extremely useful tool for .NET developers. However, with .NET 9, LINQ comes with 3 new methods. Let's begin with the LINQ Index. The Index method places every collection element against its position within that...
2 weeks ago, I went to sleep at the same time as my 2 kids.It sounded good on paper: Fall asleep before 9 PM. Wake up in the morning full of energy like a young bull. But in reality? I couldn’t sleep. I was tossing and turning in bed like a fish out of water.I resisted the temptation of using my phone until 10 PM. But then I started to scroll on Reddit. As I was scrolling, I found this question: “What are some of the craziest security vulnerabilities you've uncovered?” And this in particular...