If someone is new to programming the “test first” approach may come as a nuisance. At the first glance it introduces an unnecessary step into the work flowchart. You, a flawless programmer, are sure to get everything perfect the first time. If it compiles, it works. Right?

Not really. Getting a call about your code being a cause of a serious problem in a “production environment” is a humbling experience. It’s a lesson learned. Well, there are things you can do to avoid mistakes. If you write unit tests first, before starting work on your new features, most if not all of tests shoould fail when you run them. Once you start adding new code, new features, more and more red lights are replaced with green ones. If the unit tests cover the functionality well enough, your code is ready when all the tests pass. I may not be the best authority on testing but at least I learned that technique directly from Dean Wampler.

Enough rambling about obvious things. For the exercise I simply wrote a regular expression. I hope it is completely correct, but at least the unit tests all pass. ;-)

Update: I did not read the exercise carefully. No amount of unit tests helps if you misrepresent the problem you are trying to solve. The solution was expected to return the valid phone number. So here is version 2.