In the first part we talked about strategies for learning coding when you’re starting out. Now we’ll move on to a topic that has been the source of blood, sweat, tears, and flipped tables for many a developer…
Debugging, also known as the “why the $#!% isn’t this working?*” phenomenon.
Stuck on a Coding Problem?
It’s impossible to describe just how demoralizing it can be to try and fail at bug fixing. If you haven’t yet, at some point you will feel like a prize idiot despite your best efforts.
BUT, when you hack away at a bug and finally figure it out… oh man. Pure exhilaration will flood your veins and you’ll feel like you conquered the highest mountain.
You want more of that mountain conquering feeling, and less of that crushing inadequacy feeling, right? So let’s attack debugging from an efficiency standpoint, the same way we approached learning coding last time.
Here are a few strategies I try to follow if I can’t immediately fix a problem:
Break It Down Into Components
This is the first method I use when fixing bugs. If you’re having trouble figuring out how to solve a particular problem, try to break the big problem down into smaller chunks. Tackle the smaller chunks one by one, because they’re easier to solve individually.
This process of elimination will make it easier for you to identify where any issues are happening. Once you fix all the small problems, you’ve solved the original big problem!
Dividing a problem into smaller parts has a couple of benefits: First, it immediately makes the problem less scary and overwhelming. Fixing a series of smaller, simpler problems instead of one giant problem seems way more doable.
In addition, once you fix that first item, that small success will give you a sense of accomplishment. This will keep you motivated to continue on to the rest of the problems.
Talk to the Duck Debugging
If you try going through the steps above and are still stuck, you can try a different technique, rubber duck debugging.
To try this out, you start with an object — it doesn’t have to be a rubber duck. It could be an action figure, bobble-head, or stuffed Totoro. When you are stumped by a problem, talk to this inanimate object. Explain what your code should do and how it’s not doing it.
It seems insane, I know, but it really does work — it’s another brain hacking trick. Describing out loud how your code should function requires you to mentally trace through each of the steps. You will look at your code with fresh eyes, and you’ll often see something that you didn’t see before.
I can’t tell you how many times this approach has helped me. (Even before I knew that talking to the duck was a thing!)
At work, I would often be completely stumped by a problem after hours of Googling. Once I got to this point, I knew I needed help. I would pick my head up from where I’d been banging it on the keyboard, and ashamedly chat my boss.
We worked in different locations, so I would be typing this all out on a chat program. Of course, I’d have to describe what I was trying to do and where it seemed to be breaking. Then in the middle of typing out the problem, the solution would just pop into my head. It was always a bit embarrassing to have to say, “nvm!” in chat, but at least I had solved my problem.
So you don’t have to necessarily talk it out, out loud. You can also write out your description of the bug, like I was doing. This will have a similar effect on your brain. Nowadays I usually write out my problems on a small dry erase board I keep at my desk. It helps a ton!
Avoid Tunnel Vision and Take a Break
All right. You’ve done everything you can think of to figure a bug out: you’ve combed through Google and StackOverflow, you’ve tried breaking it down into components, and you’ve tried talking to the duck even though your officemate was giving you weird looks. No dice.
One last suggestion that may help your brain find the solution is to just…
Sounds crazy, right? I’m not saying to throw in the towel completely, but to just step back for a few minutes and give your brain a break.
How many times have you furiously tried to work something out, only to have a eureka moment a few minutes or hours later?
It’s fascinating how the brain keeps running in the background even when you’re not explicitly putting it to work.
There’s actually some science behind this. Graham Wallas, a 19th century psychologist, wrote about how the mind uses an “incubation phase,” where the mind would subconsciously work on an unsolved problem. The brain would eventually hit on the solution in what he called the “flash of success.”
So how do you get this to work and get more eureka moments? In order to leverage this incubation phase, you could do one of the following:
- Stop actively thinking and working,
- Or move on and work on a different issue.
Personally, I’m a proponent of the first option– to stop just hammering away at what’s in front of you. One method I like to use is stepping back and taking a quick walk for a few minutes.
Why choose walking, instead of staying at your desk and browsing Reddit? Well, taking a walk has some important benefits:
- Physically, it’s good for your body to not just be sitting for hours and hours.
- Mentally, the act of walking actually helps our brain to function!
This second point was a new discovery for me. You would have thought that your brain doesn’t care if you’re sitting, standing, or walking. It just needs to be fed problems and it’ll pump out solutions, right?
In reality, while the brain is like a computer processor, it’s also a biological organ. One function of this organic nature is that our brains work better with movement.
Engaging in physical activities like walking, running, or biking increases blood flow to the brain. This in turn leads to neurological benefits such as improved memory and creative thinking. These capacities are crucial to problem solving.
So the next time you’re stuck on a problem, take a quick break. It may be just the thing you need to figure out the solution.
One big takeaway from this short series is understanding that we as humans are not just machines. We can’t input code and immediately convert it into knowledge in our data banks. But if we can understand how our brains operate, we can leverage that understanding and maximize the learning process.
Why do something slowly if we can do it more quickly and efficiently, right? :)