Keeping it cool when your code breaks
Summary- When software features don't work as expected, life can get stressful
- Believe in determinism - what's not random can be worked out!
- Create a checklist and concentrate at one issue at a time to feel less overwhelmed
- Learn to follow the data systematically to find where exactly the issue is
I am not sure that I've ever met a software developer who enjoyed debugging. When things don't work, and especially if things break after they seemed to be fine,
life gets stressful. The closer to the production build you get, the more stressful it becomes to have features suddenly not working.
When it comes to debugging, there are of course technical challenges to understand and solve. But no matter what you are developing, there is also the psychological aspect that makes some developers better at debugging. I have often heard developers talk about a number of things that go through their head when they get that compile error or when a piece of functionality suddenly doesn't work:
Technical challenges aside, anxiety and panic can quickly spin out of control. You may feel inclined to call a more senior colleague for help, but admitting that you need help can also feel stressful. And sometimes, another colleague simply may not be available.
So how can you get better at debugging?
When it comes to debugging, there are of course technical challenges to understand and solve. But no matter what you are developing, there is also the psychological aspect that makes some developers better at debugging. I have often heard developers talk about a number of things that go through their head when they get that compile error or when a piece of functionality suddenly doesn't work:
- "I tend to panic about not meeting my deadlines and my brain just stops working"
- "I keep telling myself that I am not a good coder and that I will get fired if I don't sort this out immediately"
- "Seeing so many red errors in my console is just so overwhelming, I don't know what to do"
Technical challenges aside, anxiety and panic can quickly spin out of control. You may feel inclined to call a more senior colleague for help, but admitting that you need help can also feel stressful. And sometimes, another colleague simply may not be available.
So how can you get better at debugging?
Be kind to yourself
First, be kind to yourself and try to remember that there is no perfectly written code. Even the most senior programmers constantly run into problems.
So, while technical seniority can certainly help, you can get better at debugging no matter your level of experience.
What's more, the more calm and patient you are, the easier your debugging task is. So make a fresh cup of coffee, put your favourite music on, and get into your hyper-focused state.
What's more, the more calm and patient you are, the easier your debugging task is. So make a fresh cup of coffee, put your favourite music on, and get into your hyper-focused state.
Believe in determinism
Try to assure yourself that you can solve this problem. Most code is completely deterministic, unless, of course, you are generating pseudo-random numbers
to guide functionality. But that's extremely rare.
Determinism means that there is a specific root cause to everything and with enough patience and attention to detail, you can find that cause and solve the issue.
Determinism means that there is a specific root cause to everything and with enough patience and attention to detail, you can find that cause and solve the issue.
Create a checklist of issues
Once you are calm, confident and collected, it's time to disassemble the problem into pieces. Creating issue lists is an excellent way to reduce the feeling of being overwhelmed. Just imagine selling sandwiches in a cafe. Would you rather have ten people shout out what they want to eat at the same time, or would you like to have an ordered queue of people to serve one by one?
If you work with compiled languages like C++ or Java, you may see a number of errors at the same time. Ignore all but the first one. Seriously, do not even read the other error messages. Concentrate on solving the first problem, then the next. Sometimes even, errors stack up and if you solve the first one, the others disappear.
If you work with dynamically typed languages like Python or JavaScript, you usually run into issues and bugs during run time. Again, disassembling the issue into smaller pieces is the key. Try to find the most simple, repeatable steps that cause the issue. Note down other issues that you are finding along the way, but forget them for now. Solve the first one, then the next one.
Another advantage of following a checklist of issues is that completing each gives you a sense of achievement and progress. Do a little celebration in your head every time you solve a bug - it's one more step towards great-working code!
Learn to follow the data
Unless the problem is completely obvious, like forgetting to call a function or misspelling something, in my experience, most bugs come down to data flow.
You think that a certain thing is happening with your data, but a mistake in the code makes it not so.
As a result, the final behaviour is different than expected. It is really this simple.
One of the best skills that you can invest into learning as a programmer is patiently and systematically following the data. Put breakpoints or console print statements everywhere that's even remotely related to the issue that you are facing. Are the correct functions being called? What are the function inputs and outputs? This narrows down most debugging into the following steps:
Of course, steps 2 and 3 could take hours, especially in complex code bases. This is why good coding practices are important. The less convoluted the code is, the easier it is to follow the data.
As a general rule though, it is helpful to keep in mind that usually, there is a single line of code that causes most of the problems.
One of the best skills that you can invest into learning as a programmer is patiently and systematically following the data. Put breakpoints or console print statements everywhere that's even remotely related to the issue that you are facing. Are the correct functions being called? What are the function inputs and outputs? This narrows down most debugging into the following steps:
- 1. Find how a variable is first initialized
- 2. Find the precise point in the program execution where its value does not match what you expect
- 3. Fix that and you're done!
Of course, steps 2 and 3 could take hours, especially in complex code bases. This is why good coding practices are important. The less convoluted the code is, the easier it is to follow the data.
As a general rule though, it is helpful to keep in mind that usually, there is a single line of code that causes most of the problems.
As a former software developer, I enjoy helping techies with increasing their confidence and with developing their careers.
Learn more if you need support.