3. 11. 2020 • 3 min read
Validated: Sep 2024
This is the first part of our small series of tips for junior developers focused on having a systematic routine while debugging the code.
As we’ve seen with many junior developers - and have gone through it by ourselves as well - when we end up with an error, we panic and start doing random quick changes in our code to see if anything would make the error message go away. However, this approach does not fix any bugs, but unfortunately brings even more of them into the code. This is because we are trying to quickly cover the obvious symptoms rather than spend time to understand the root cause. Consequently, the problem is solved only partially and our quick fix can lead to other issues. The whole process then becomes a never-ending circle where fixing one thing crashes another one.
Instead, it’s much better to calm down, take a deep breath, and start with a systematic approach.
The first step to successful debugging is reading the error message. We know, it's obvious. However, properly reading the message can tell you what file the error is coming from. Specifically, it can tell you what line the error is coming from. This provides you with a great starting point to examine and ultimately fix the problem. This is the first and most important step with which you should start every debugging as it will save you a lot of time as well as your mental health.
Traceback (most recent call last):
File "start.py", line 29, in main
router.resolve()
File "app/router.py", line 41, in resolve
controller.get(request)
File "app/controllers/article.py", line 89, in get
response += f'<h1>{article.title}</h1>'
AttributeError: 'dict' object has no attribute 'title'
If you are not finding the error by reading the error message, the next thing you should do is to systematically eliminate possible sources of the error. This means, start at the beginning, take a small part of your code and make sure it behaves correctly. Once confirmed, start adding other smaller parts to it and check again that it works as you would expect. Unit testing can greatly help you with this process of elimination. By systematically sorting the good parts of the code from the bad ones guarantees that you will find the cause of the problem and make the debugging much quicker.
It’s also important to be fresh and not to get stuck with the small part of the code forever. We know it can be frustrating to be stuck on one problem and not seeing the solution but really don’t waste your time on thinking: “It’s impossible”! Sometimes it’s just better to ask google or your colleagues. :)
Once the code is working it’s a good idea to look at it as part of a bigger picture and make sure that code metrics such as Cyclomatic Complexity, Coupling Between Objects, etc. are at a reasonable level. By doing so you are making the code more readable and maintainable which helps to prevent many bugs or makes finding and fixing them faster.
Codeac helps you guard all the important metrics and many others to give you actionable feedback and insight about the current state of your codebase. This will help you keep track of your code quality and will help you to be an even better developer!
Outstanding developers make the process mentioned above as part of their routine. This means every time a bug occurs, they stay calm, and know exactly what has to be done thanks to the step by step approach. So remember, read the error message thoroughly to locate the issue, if it doesn't help start working with a small functional part of the code and add more to it while making sure the code still works. And finally, don’t hesitate to ask your colleagues when you get stuck. After all, we all want the same thing - working code that’s clean and without any bugs.