Posted by: CompleteITProfessional
If you work as a software developer, then you’d be familiar with the necessary yet sometimes difficult task of debugging your own code. This task rarely goes as well as you’d like, and you can often spend hours trying to fix a bug or reproduce a problem in some code. Try out these five tips that I use when debugging code.
This is quite an obvious tip, but I don’t think it’s used as often as it should be. Breakpoints are available in most IDEs and they allow you to place a stop point on a line of code. When running the debugger, the code will run and stop at the point that you specify. This is a handy time saver, especially if you have a lot of code to test. It can help if you’re testing some code later on in your process, and you don’t want to go through all the initial code manually.
It’s also helpful if you’re running the same piece of code over and over again, such as inside a loop. Setting a breakpoint inside a loop will allow you to inspect the code and variables during each iteration.
Write Unique Output Values
One of the most helpful things that I implement when debugging is to use the IDE’s output function. Depending on your language and IDE, there should be a system output window or tab that is available for you to write data to. You can add lines of code into your application to use this output.
It’s useful for writing variables to see what they are at certain points. It can help to check if there are any strange characters being added or if there is values missing. It’s good for tracing the path of the code execution. If you give each output call a unique value, you can see which ones are written in the output and match them back to the code.
Set Watch Values
Watch values are used in IDE’s to display what a variable is set to at the current time. It’s helpful using them with breakpoints as well as when debugging the code line by line.
You can see what a value is set to, and also keep an eye on it when it changes. It’s helpful for variables that cause the code execution to change, such as those used within an IF statement. The way to do this depends on your language and IDE, but most major languages should have this function. You should also be able to see what data type a variable is, along with what it is set to. The value of the variable doesn’t always indicate the data type, which can be handy to know in your code.
Prepare Test Data
This is most helpful when you need to run several tests to find a problem in your code. Sometimes you need to set up some data to run a few debugging tests to work out what the problem is. Depending on how your code is structured, you should be able to set up some test data to use.
This will depend on what your code does and what language it uses, but using some test data can be beneficial and save you time when doing repeats of debugging. As a software developer, you can set this up, or you can ask someone else to if they’re more familiar with the input area or the area where the data is loaded from.
Ask Someone Else
If all else fails, you might need to ask someone else to have a look at the code you’re having issues with. Sometimes we can get too “close” to our code, spending hours or days working on it that we miss some major details or flaws.
Ask someone else in your team or someone who is familiar with the program to take a look at your code and see if they can work out what the problem is. I’ve been on both sides of this issue before, looking at someone else’s code and also having someone look at mine. Don’t worry about looking silly for not picking up a mistake. Looking silly is a small price to pay when you save all this time.
Hopefully these tips have helped you debug your code. What tips do you have for debugging the code you’ve written? If you’d like more IT career advice, visit my site at www.completeitprofessional.com.