You Should Avoid Debugging
Disclaimer: debugging is one of the most important techniques in the toolbox of every software developer. Investigating issues in your code by interspersing it with “print” statements or slightly changing it until it works is rarely a good solution, so “learn how to debug” is the best advice you can give to a less experienced developer.
And here I am, saying that you should stop debugging. Yes, as soon as you learn how to debug, you have to immediately start to un-learn it.
Debugging is a great technique, but it should be used sparingly. If you blindly jump into a debugger every time you face a problem you don’t immediately understand, you risk deprecating your reasoning skills.
I argue that debugging should be your last resort. When I use debugging, for me this is the sign that I’ve failed to understand what’s going on in the code, so I feel ashamed of myself. (Just kidding. Almost.) I try to at least reflect on why I had to resort to debugging.
A good idea is to take a step back and try to figure out what can be improved in your code or your understanding of it so that you wouldn’t have to debug it in the first place. In this article, I will concentrate on just three simple pieces of advice that can help you avoid debugging altogether.
Use Proper Logging
Whenever you don’t understand the program behavior from its logs, this means your logging has failed you. Think of what can be improved. Typical problems with logging include:
- there’s not enough logging statements or no logs at all — add a logging framework or a logger to a particular class or module, think of what you were missing that could have helped you to troubleshoot the issue without debugging;
- an error or an exception was silently swallowed, or a new exception was re-thrown without connection to the original one — always take great care when working with exceptions, try to figure out how and when each exception that you throw will be logged;
- a line in the logs didn’t contain enough information about the context; a tell-tale sign is a log message without placeholders, for instance, something like
log.info("Doing work")instead oflog.info("Doing work with object {}", object).
Also, make sure you know how to enable debug logging or change logging levels in your application without restarting it — modern frameworks allow that. For instance, Spring Boot provides an Actuator endpoint to change logging levels at runtime.
Know your language and APIs
If you work constantly with some programming language, it’s worth investing your time into learning its syntax, specification, standard library, multi-threading facilities, memory model.
This knowledge should not become stale if you constantly use it. Looking at a piece of code (whether written by you or by someone else) and clearly understanding how and what it does is one of the best experiences you can have as a developer, next to solving some complicated task yourself.
If you use a third-party library, take your time and read its documentation. If you build your application code on top of a framework, like Spring or Hibernate, take care to read at least one good book on it. Go through the framework’s documentation at least once. Understanding the inner workings of a library or a framework can improve your general understanding and save you hours of debugging — the meaning of a particular error or behavior could be immediately clear to you if you know what happens under the hood.
Learn to reason about the code
Probably the best technique to avoid debugging and improve your understanding of the code is to run it inside your head or on a piece of paper. This may sound tedious at first, but this is a skill that can and should be learned.
A good way to learn it is to solve coding tasks (like the ones on LeetCode or HackerRank) without using any additional tools. Not even a code highlighter. Solve a task in Notepad first, verify it in your head or with paper and pencil and then try to run it. Note what kinds of errors you miss: not remembering some particular method signature? Forgetting tricky parts of syntax? Edge cases? Be more attentive to these particular problems the next time.
Honing the skill of running the code inside your head or on a piece of paper improves your reasoning capabilities, allows you to find errors just by glancing at the code, avoids tiresome debugging, and lets you in most cases get the code right the first time. Fewer errors mean less debugging.
Conclusion
Debugging is a great technique, but when applied sparingly, and only as a last resort. Improve your code and develop skills that help you better understand it, and avoid debugging when it’s not needed.