7 ms·
TDD without refactoring is like sex without orgasm. Okay, maybe it's not that bad.. ;) Seriously, the most fun I ever had with TDD was when using a language th
by briantopping 12y ago
TDD without refactoring is like sex without orgasm. Okay, maybe it's not that bad.. ;)
Seriously, the most fun I ever had with TDD was when using a language that my IDE had great refactoring tools for. Just write all your code inside a test to start with, don't bother with production anything. The test becomes a little like a command line program, launches immediately from the IDE and you can iterate super quickly. Your output is whether the test (with the test data) passes or fails. I'd pick the test framework by whatever the IDE liked best. Pursue solutions that viscerally satisfy until you know better.
Once you have a test working like that, do "Extract Method" followed immediately by "Move Method". The destination of the move is the production class. With two menu selections, you have a perfectly testable interface, called perfectly by the test. A good extract refactoring always proposes parameters for the method and lets you order and rename them. It will also recognize extractions that can't work, like when two results are generated by the selected code. In that case, extract more than one method and move all the methods to the production code.
Want to add more code? Do the same thing again and again and again, each time as a new test.
When you are done, you'll have production classes that are the composite of the extractions and all the tests that once contained inline code (but now contain method calls to the production code).
At some point, you will ask "how can I improve my tests?" The answer is easy: Code coverage tools. You should shoot for > 80% of your production code having test coverage. Some say more, few say less. Make some rules for yourself about things you don't bother testing, like getters and setters, use the 20% leeway so you aren't flogging yourself. Keep having fun, don't make a death march out of testing every last line of code.
This habit becomes really fun. You'll start telling your friends. Nirvana will ensue.
- sklarsa 12y agoWhat IDE did you use? Can this workflow work in Visual Studio using NUnit?
- mrbrandonking 12y agoReSharper is an add-on component for Visual Studio that is (IMO) the best refactoring tool available for any IDE. ReSharper includes an NUnit-based test runner, too. JetBrains is the company that makes ReSharper. I also use AppCode (their alternative to Xcode) in a way that's similar to how I use ReSharper. I do most of my iOS development in Xcode but load the project in AppCode before committing so I can take advantage of its superior code inspection and refactorings.