It happens to everyone; you spend hours - if not days - pouring over your code, tweaking, testing, and re-running your code, only to hit error after error. Finally, you realize you’ve indented a single line of code incorrectly. You are relieved, elated, furious at yourself, and emotionally exhausted. Here’s how to bug fix while maintaining your sanity.
Step 1: Understand the circumstances.
Ensure the bug is reproducible. In other words, can you predictably cause the error to occur? Most of the time, this isn’t a problem since the bug may prevent the code from compiling altogether. However, if the bug only occurs in certain circumstances, the first step would be to test every edge case to determine how the bug behaves in every situation.
Step 2: Determine the type of bug you’re dealing with
There are a four places in which errors can occur:
- Syntax errors - prevent the code from compiling altogether. These are the easiest to fix! Usually a quick google search will fix any language or package errors and get your program compiling.
- Runtime errors - occur while your program is running and cause it to exit prematurely. These are usually caused by an error in variable in aliasing or mutation, which can make your input invalid. Keeping your code organized and relatively independent really pays off here, so you can examine individual functions and make sure your variables aren’t causing any issues in your program.
- Logic errors - are difficult to catch because they won’t stop your code from executing, but they will cause an unexpected or undesirable output. Here you’ll have to look at your program more holistically, then walk through the code to see where things go wrong. This might be a good time to stretch your legs and take a break. Unless you’re really “in the zone,” taking a break will help you refocus and tackle the problem at hand.
- Server/package errors - only occur when handling and using code that’s not the pure form of a language. These errors can be harder to understand if you’re not familiar with the other server, and in worst case scenarios may just be out of your control. Consider putting in a development request or asking for help on a documentation forum.
Step 3: Find the error source
Your console will link you to the line of code in which your program was terminated, but oftentimes that line isn’t the actual cause of the issue. Use logging statements (print, log) to trace previously referenced variables to make sure they have valid input. Print descriptive comments - eg, print(“Yearly profit: “, profit) - so you don’t get lost in a bunch of random values. If your editor/IDE has a debugger, it can be extremely useful to go through the code step by step and see all the defined objects and variables. Paying close attention to how the code jumps in and out of your code should be able to lead you to the source of your error.
Step 4: Fix it!
Of course it’s not as simple as it seems, but you’ve done 99% of the work now and you’re almost there. Remember that at any point, if you feel frustrated or overwhelmed, it might be beneficial to take a quick breather. There’s a difference between “being in the zone” and forcing yourself to work for hours. A study by the University of Illinois at Urbana-Champaign found that brief diversions from a task, especially those that require focus for prolonged periods of time, vastly improves focus.
Tips to maintain sanity:
Take necessary breaks
Forcing yourself to keep working at something might be more detrimental than it is productive. Once you take a break, you may be able to see the solution clearer. If you’re really stumped, take a few days to completely forget about your code, then come back with completely fresh eyes!
Get someone else to take a look
It’s always good to have someone else look at your code even when you’re not bug fixing!
Healthy body, healthy mind
Thinking capacity is often directly linked to hydration. If you find yourself feeling like you want to beat your head against a wall (or throw your computer out of the window), maybe you’re just dehydrated (or sleep deprived, or hungry). Get adequate sleep, nourish yourself, and take a quick walk.
Go old school
If you’re stuck in a bit of a rut, grab a pencil and some paper and start diagramming out your code! Write down some test cases and follow through all the functions and loops to see how it mutates. Draw your data structures and what the values look like. That’ll make any inconsistencies much more obvious.
Believe in yourself
Just kidding, kind of. If you feel overwhelmed, take a step back and affirm that this bug is fixable and there is a solution. Sometimes, this is enough to refresh your mindset and get in the zone
Tech Talent South (TTS) offers a one stop shop for flexible tech talent solutions for teams of all sizes and at all stages. Whether it’s for organizations looking for top quality talent at scale and with speed, Workforce Development initiatives, or companies looking to future proof their own employees by giving them new or advanced skills (upskilling and reskilling) - we deliver solutions that fit every need.
We offer scholarships for technology education courses such as programming and data science to underrepresented folks in tech; including Black, indigenous, people of color, women, and veterans. Our goal is to help close the diversity gap by training based on aptitude, not access.
Interested in building your skills? Download a free program overview for an upcoming course here. Learn more about our scholarship application.
Interested in building your team? Connect with our team here.