Stop, Think, Fix Errors... (Repeat!)
"Don't change your code unless you know how it will fix your problem". If I had to pick a quote to put a 'squiggly line and my name' next to, then this would be it.
I've seen developers stare blankly at an exception message, comment out a line of code or two and then try and run their code again hoping for the best.
This, of course, rarely fixes anything. Quite often the line of the error message has little to do with the actual problem. Instead, this introduces new errors into your code and causes the problem to take longer to resolve.
Even if you guess and make an exception message go away you still don't know what caused the error to begin with. Instead, you have a new error that is quietly causing problems and is now much harder to find. They might be ugly, but exception messages are the easiest errors to fix... well, after compiler errors that is...
So, how can you "know" your change will fix the problem? Here are some of the recommendations I make to other developers.
Stop and Think... Carefully
The bug isn't going anywhere and each time you guess incorrectly you add a new error to your code.
It seems like such a silly thing to even mention, but I've seen plenty of developers take a cursory glance at an exception message and then just start changing stuff. It doesn't even matter if you're in a "break-fix" situation - you should always carefully consider the problem before changing your code.
Read the error, start following up the stack and think about what could cause this result. Think about the conditions that would cause the error or how you could force the error to happen again.
If you don't know why your change will fix the code then you aren't fixing anything.
Understand the Debugger
You don't need to know what a memory dump is for or how read binary manually to use debugger. Just simply understanding how to set break points and stepping into, over and out of code will probably cover 99% of the problems you encounter.
It might seem daunting at first but don't ignore the debugger because logging or message boxes are easier to write. Once you get a hang of it, the debugger will most likely become the greatest tool in your future bug squashing adventures.
Isolate and Resolve the Specific Problem
It can be difficult at times to figure out why a section of code doesn't work when it sits in the middle of a larger application. Sometimes its better to create an empty project and work with a smaller section of the program by itself.
For example, if you don't understand how a part of your framework is supposed to behave, pull it into a separate program, for example a console application, and work with it until you are comfortable with the results. Then when you integrate your changes back into the main project you can be confident that any remaining errors aren't part of the newly introduced code.
Look At Open Source Code
If you've written some code that is consistently causing problems then consider looking for a framework or open source project instead.
This isn't a matter of being a good or bad developer. It certainly has nothing to do with being unable to figure out the problem for yourself. Quite simply, code that consistently has errors is probably a difficult problem.
Public frameworks and open source projects are typically owned by other developers that understand the problem better than others. They have taken the time to abstract away the specifics of a problem and build a clean API for other developers to use.
These developers are also typically eager to improve their code and will listen to feedback. Even if you don't use their project there's a good chance that you will learn something by simply reading their code.
To sum it up, don't just change code and hope for the best. Stop, think, ask yourself how does this fix the problem?.
How do you approach fixing problems in your code?
June 23, 2010
Stop, Think, Fix Errors... (Repeat!)
Post titled "Stop, Think, Fix Errors... (Repeat!)"