Often in coding, I find it very slow and difficult to detect the root cause of a bug and sometimes I end up going to wrong point in my code. It's painful. I know that to detect the root cause of a bug is a very important skill for programmers. Does anybody have a trick or technique to suggest a good way of finding the root cause?
How to detect root cause of problem or bug
code quality;problem solving
Ask why a lot, and keep asking it until the problem is clear.Why did the code crash? Because we got a divide by zero.Why did we get a divide by zero? Because X was zero.Why was X zero? Because it was passed in by function foo.Why did foo pass in a zero? Because it was set to the total number of relationships that the object hasWhy did this object have zero relationships? Because that's the default value before the object is fully initializedWhy was was the value not set to be greater than one during initialization?There is a set of cascading if statements without a final catch-all else statement.Why did this object not satisfy any of the if statements? Because the code assumes that every object has at least one parent, or at least one child. Is that a valid assumption? This technique is often referred to as 5 Whys. You can read more about 5 Whys on wikipedia. You might also be interested in reading answers to the question What is your most useful technique for finding (or preventing) bugs?