A recipe to help you find error in your program, when nothing else works

Sunday, October 2nd, 2011|Beginner tips|by Romans

Once in a while, I see people who are stuck with a problem and who say they have tried everything and it still does not work. I often find myself in a similar situation, but I have found a recipe to resolve it.

If your program does not work, comment/delete it completely

If you have tried everything to make your program work, have you trying to remove the source code from your file completely (you can use Undo after)? Make your program exit right away or comment out everything. Will it work? Try it.

Does it work now? Yes. Now we can’t say anymore that “nothing works”, something actually worked so we are on our way to find what breaks it.

Divide and Conquier

Next, let’s delete the half of your program. Of course commenting it out or putting “exit” in the middle of your code also would work. Comment out half of the elements especially the ones which you think are giving you trouble.

If your problem is back, then comment more of your application and continue like that until you have removed all of your application. Because removing application completely worked before, somewhere along the way the problem will go away.

If your half-commented version worked, un-comment more of the code until problem appears.

Isolate your problem

Now that we know what breaks it, let’s find out “why”. This time, remove code which is not relevant to your problem. Even if you already confirmed that it worked fine, remove it and only leave the problematic bit. This way we would know that nothing we assumed to work does not interfere with our problematic section.

When you have removed everything un-necessary and you are left with several lines of code containing the problem, there is one last thing to try.

Rewrite it

Comment your code out and start typing it again. Even if it seems silly, still re-type your code. Do a test-run of your code after each line.

Conclusion

There you have it. At one of the steps above, you would certainly have found your problem. If you did, then press “UNDO” several times to restore your program in the way how it was before.

Happy coding!

1 Comment

Qambar
Posted August 16, 201311:28 am

It helped alot ! Thanks !