I was browsing StackOverflow when I encountered this question. Here the author mentions his/her style of debugging:
I am wondering how to do debugging. At present the steps I follow is,
- I complete a large script,
- Comment everything but the portion I want to check
- Execute the script
and in one of the answers another user says that the asker is debugging the wrong way:
Your sequence seems entirely backwards to me. Here’s how I do it:
- I write a test for the functionality I want.
- I start writing the script, executing bits and verifying test results.
- I review what I’d done to document and publish.
I’m fairly new to programming, and I follow the first way of doing things. It seems that the second way is called Test-driven development and it seems to be a very inefficient way of doing things.
Can you explain TDD and its merits in a simpler way?
So do many others
All new and different things seem inefficient. Get used to the feeling that everyone else is wrong and you’re right.
The rest of your career will be filled with this feeling. Every new things seems inefficient. And will always seem inefficient.
Yes.
TDD is more efficient because it’s more efficient.
You must do the testing. You can write tests first or last. Either way, you have to write them.
You can “Comment everything but the portion I want to check” and try to locate bugs in that in a slow, ineffective way. It’s often ineffective because — without tests to drive your development — you may write code that’s useless or a waste of time.
Or you can write a test and write the least code that passes the test in an efficient way.