Monday, July 23, 2018

General Tips for Debugging

In this post you will learn about code debugging and some exceptionally useful general tips for debugging.Debugging is the process of detecting and removing of existing and potential errors (also called as ‘bugs’) in a software code that can cause it to behave unexpectedly or crash. To prevent incorrect operation of a software or system, debugging is used to find and resolve bugs or defects. When various subsystems or modules are tightly coupled, debugging becomes harder as any change in one module may cause more bugs to appear in another. Sometimes it takes more time to debug a program than to code it. 

To debug a program, user has to start with a problem, isolate the source code of the problem, and then fix it. A user of a program must know how to fix the problem as knowledge about problem analysis is expected. When the bug is fixed, then the software is ready to use. Debugging tools (called debuggers) are used to identify coding errors at various development stages. They are used to reproduce the conditions in which error has occurred, then examine the program state at that time and locate the cause. Programmers can trace the program execution step-by-step by evaluating the value of variables and stop the execution wherever required to get the value of variables or reset the program variables. Some programming language packages provide a debugger for checking the code for errors while it is being written at run time. 

The following are the debugging process: 

1. Reproduce the problem. 

2. Describe the bug. Try to get as much input from the user to get the exact reason. 

3. Capture the program snapshot when the bug appears. Try to get all the variable values and states of the program at that time. 

4. Analyse the snapshot based on the state and action. Based on that try to find the cause of the bug. 

5. Fix the existing bug, but also check that any new bug does not occur.



General Tips for Debugging:

Get to know your debugger. If it’s not an IDE, learn how to point it at your source files and compilation information required to do source-level debugging. Create a very simple program that calls one routine located in a second source file. Make sure you can set breakpoints and learn to use the core commands. Make sure you know the difference between ‘step into’ and ‘step over’.

Program with debugging in mind. In other words,
  • Consider having intermediate values for complex expressions.
  • If appropriate for your language, separate object creation and object initialization.
  • Be cautious about using templates, macros, and other constructs which auto-generate relatively opaque code.
  • Learn the difference between optimized and debug versions of code execution, if they exist in your language. Consider including debug-only code statements for code that only shows up in the debug compilation. For example in C++ you would use #ifdef _DEBUG.
  • Add logging to your program. Use ‘log levels’ which you can configure with a static variable to increase and decrease the amount of information logged for troubleshooting.
  • For any pseudo-random generators used, save the seeds so you can hard-code them if you need to reproduce a particular outcome.


Learn how to set conditional breakpoints. As the name implies, conditional breakpoints are only triggered when a condition is set. For example, on a loop from 1 to 10,000, if I know there’s a problem at iteration 7143, I can set a conditional breakpoints for the first statement of the loop when the iterator is equal to 7143. It’s slower than normal execution, but still a HUGE time saver, compared to hitting ‘go’ 7143 times.

Learn remote debugging. Many debuggers allow you to remotely connect to a process - even on another machine - and debug it. This is invaluable for debugging full-screen and focus-sensitive applications.

If your IDE supports debugging, learn it! If you can build and debug within your IDE, you’ve already got all the source searching tools you’re used to. It’s a great way to go. If you can figure out how to do this with less than a week of effort, you should.

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...