FizzBuzz C#
I was recently having a discussion with a friend about hiring practices and sharing with him stories about different technical tests we have had to do. Some exam based with multiple choice and others where we actually had to build something. He being a contractor, didn’t have much of a technical test for his current role (judge that how you will).
For my current job I told him that I had to provide a solution with lots of things you would expect to see in a modern green field app today: unit testing, dependency injection, good separation of concerns and good use of the solid principles together with a final report explaining why I did what I did and pouring over my thoughts with what I may have done differently after I finished the test. This was then code reviewed by a senior followed up with questions in my interview.
After that he then asked me if I had ever heard of the “FizzBuzz” test. He told me that he had once been asked to do the FizzBuzz test and that it did seem to weed out quite a few people but not only that you can definitely see how people code, like the FizzBuzz test is a lens into the minds of a developer. Do they prefer imperative or functional, how do they name their variables. To top it off it can be performed in any language.
In truth I had never done the test, but I had briefly come across it. While watching an NDC talk by Mark Seeman on Youtube entitled “One Kata, three languages” where Mark performs the kata in three languages, once in c#, then Haskell and finally in Clojure (I was actually browsing round searching for videos on Clojure), anyway I didn’t watch the complete video.
I decided to do some googling and came across a discussion on coding horror about why programmers can’t program (https://blog.codinghorror.com/why-cant-programmers-program/). The embolden text in the blog says “Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes. Want to know something scary? The majority of comp sci graduates can’t. I’ve also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.”
I find this blog to be quite disturbing, though sadly unsurprising. I have found there is quite a varied level between graduates and not only that senior developers when it actually came to the day to day practice of writing code. With senior developers that is another story but with graduates, I feel that if anyone leaves university (or even a college course) not being able to perform this task then they have been truly let down.
Anyway… I decided to give it a shot. I looked at the requirements on coding horror and read “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””
Seemed simple enough and I broke out Linqpad (funny really how before even taking a breath on how I might actually solve the problem that I started up a program that had Linq in mind, first insight into my coding style perhaps).
Straight away I wrote
Enumerable.Range(1,100)
.Select(e => (e % 3 == 0 && e % 5 == 0) ? "FizzBuzz"
: e % 3 == 0 ? "Fizz" : e % 5 == 0 ? "Buzz" : e.ToString())
Not bad but I didn’t like all the ternary operators, not easy to follow, so I extracted out the ternary’s in the select into a function called “FizzBuzzNumber”, in doing that I also decided against the ternary, I think readability is compromised when you go more than one or two layers deep without some formatting (which I could have done with my Linq statement but I prefer each part of the Linq chain to be as expressive as possible without breaking out into multiple lines).
That then changed to this:
void Main()
{
FizzBuzzTest();
}
public static void FizzBuzzTest()
{
Enumerable.Range(1,100)
.Select(FizzBuzzNumber).Dump();
}
public static string FizzBuzzNumber(int number)
{
if(number % 15 == 0) return "FizzBuzz";
if(number % 5 == 0) return "Buzz";
if(number % 3 == 0) return "Fizz";
return number.ToString();
}
Also I should mention that I realised that the lowest common number to 3 and 5 was 15, I could then just check the modulus of 15 and reduce the complexity of the first condition. I later took a peak at how Mark Seemann had done it, he had used “I % (3 * 5) == 0” nice.
That seemed to be that however, the problem is really only half finished. I discovered browsing the kata that some people had then taken it one step further and wanted to print the results into a single line. I then did this using aggregation like:
.Aggregate((x, y) => string.Join(" ", x, y))
In summary this was a nice 10 minute distraction. Looking here http://codingdojo.org/kata/FizzBuzz/ it seems that this should really be used as a introduction to TDD. I do hope that most developers can write this before applying for a development gig, the pain of not being able to do this while sitting there looking at a user story or requirements wondering what to do would be unbearable.