Reporting Bugs
Anyone can say I just found a bug that crashed the software but that's pretty useless. What us devs need is a nice set of steps to recreate them. Without this it could be almost impossible to track. So, for you folks reporting bugs please try the following:
- When you report a bug, please include a detailed description of what you were doing at the time. Logging it as 'It crashed when I clicked the mouse' doesn't help. Have, I opened form X and left-clicked on the button labelled 'Do this' and the software crashed - that helps
- Data please. If the application has a project file, it's a good idea to include this. If you're importing data from a file, include the file. If the application uses a database, get a backup if possible. Not always necessary if the "bug" is a typo on a form or something
- Some systems (like the one I'm working on) uses loads of generated files in the background and so a backup of the working folders really goes a long way.
- Screenshots of the error can help.
Recreating the Bug
For a developer, sometimes this isn't necessary as you know right away what the cause is, especially a visual issue. I've frustrated many a developer by saying on more than one occasion "Hey, that button is off by 1 pixel, can you fix it" or "the colour is off, it should be darker/same as that other label" and they get really annoyed. Anyway, recreating the bug is good and here's a few hints as to how you might do it.
- Get the user's data. This is really important. Sometimes it's not possible but that isn't always a problem if you can recreate the bug.
- Start at the lowest level in the code and check the logic. If you can debug the code, step through working your way up.
- Add traces to the code. Depending on the development environment this may or may not be easy. Visual Studio has a TRACE function which outputs to a Output window inside the IDE and you can view the results. This is really useful.
- Dump a trace to a file. Just make sure you back the code out OR, in the case of a user not being able to provide you with data for confidentiality reasons, give them a debug build with this additional diagnostics and ask them to send the results.
- a series of small number representing the length of an item that is composed of small sections as little as 0.3 and 0.23m when summed did not add up to the actual length. The problem was caused by C++'s accuracy when dealing with numbers. 0.3 became 0.2999999997 and 0.23 became 0.22999999996. We were using double precision as well and we even created a simple command line program and assigned 0.3 to a double variable then output it and it was wrong. The cumulative error resulted in failure of the system
- Adding tabs to a form using MFC (never a good idea, MFC not tabs). The software crashed when removing a tab that was no longer used. The creation order was the cause and the last tab had to be created first.
Fixing Bugs
There are a few useful techniques when fixing bugs. some people I know rely on catching the errors and dealing with them if an exception is thrown but that should be the last resort. Here's my recommendations here:
- Initialise variables to something you know is invalid and check them. e.g. pointers to NULL and check they are valid.
- Following from 1, code defensively. For example, if you open a file, check the file handle is not NULL before proceeding - don't assume it's open. Generally, check results at every step where results can go wrong, because Murphy says they will.
- Any methods/functions you write that can generate incorrect results or errors should return error codes so higher up code can handle these errors. 2 is a recursive process.
- IF and only IF there's no other way, try-catch is a good way to prevent a crash and at the very least fail gracefully. There's a few cases where they are absolutely necessary as a last line of defense. Sometimes a routine can execute and fail with an exception and the next time it'll be fine. If you have a system that needs to be up 100% of the time or as close to that as possible then the try-catch is a life saver...but as a last resort.
Happy hunting.