An often overlooked approach to fixing broken code, is to simply delete the broken part and rewrite it from scratch. It might seem counter-intuitive, and a lot of people would be hesitant to delete code that’s already there. It might seem like a step backwards from the solution, but in certain situations it might be required to move further forward.
Sometimes we get lost in code. Sometimes we get really lost in code. This often happens when a given block is hacked together out of various parts — a template was copied from the sample, a buddy has “helped” with a part, someone has posted something on the forums… There’s a mess of a function, no one knows how exactly it came to be or what exactly it does. Such code often produces bang-your-head-against-the-wall type of bugs.
Rather than fixing a broken mess, sometimes it might be simpler to start from scratch. Unusable lines of code don’t bring one any closer to the solution, so removing them should not be taken with difficulty. It’s a tradeoff in effort between understanding faulty code well enough to fix the problem, and writing your own code (which you presumably understand since you wrote it yourself). Of course this works better when there is less code to rewrite, so a reasonable effort should be put into narrowing down the location of the bug.
It happens that my own code is at fault. It might be a sign that I didn’t allow for enough thought before beginning to type. So instead of getting frustrated with an elusive flaw — sometimes a block of code gets scrapped. Now, having thought more about the problem and having experienced the associated pitfalls, while writing and fixing the previous attempt, it’s often easier to come up with a new, more elegant, solution.
In programming, it is important to understand your instructions. Lines of code that hinder this understanding should be removed.