Interesting story here about an interview problem called FizzBuzz. The author make the surprising claim that most interview candidates can't solve a simple problem called FizzBuzz:
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
It took me about 9 minutes to write the FizzBuzz.java file and another 5 minutes to write the FizzBuzzTest file. The application ran immediately and all the tests also passed the first time out of the gate.
From the outset, I went with a modular design separating real functions from those with side effects. The function getOutput is responsible for determining what string to output for a given integer, and there is even a printNumbers function, which does the looping and printing, so as to keep the main function as simple as possible.
When I wrote the tests, I remembered that I had to make the getOutput method of FizzBuzz public so that I could write a unit test for it. After some googling, I see that there are test frameworks like Mockito that allow testing of private methods via the use of special class loaders. Will have to try that in the future as I don't like having to change the class under test just to make its methods testable.
No comments:
Post a Comment