FizzBuzz - my first interview task on whiteboard

 I think we all know the FizzBuzz task, when we a function shall print fizz if the number is divided by 3, buzz if by 5 and fizzbuzz when by both. Yesterday I did a small research about how many different implementations we can have using C# and there were many. Ones looks very common with "if"  statement tree, some of them were using LINQ and looking hard to understand what is going on in a first instance.

StackOverflow has this one question with a bunch of examples Writing FizzBuzz, that I will use to check how those solutions work from a performance perspective, code complexity and human understanding.

listing 1 fizzbuzz question

As we can see, OP introduced a tuple to solve the problem, which in my opinion gives a lot of opportunists for extensions with a little code changes. 

The metric looks good, but if we move the list declaration outside the function (so it is not recreated every time the function is called we shall be good)
To do benchmark I will convert all those functions to return string for a given integer, so you can see this file https://github.com/profesor79/FizzBuzz/blob/main/01MareInfinitus.cs



As we can see, the function performs great unless we have fizz buzz case, as it needs to add those strings and that takes a lot of CPU time, and moving the declaration outside a function increased our maintainability index:


Let's grab other example of FizzBuzz from that thread 

As author claiming it does the job, but it will be hard to extend in an easy way if we need to cover more cases, but the metrics (after changing to return the result as a string) are great:

Now as we are returning string value, I got rid of the "else" statement and magically we drop our index as the number of code lines increased (imho that is a shame...)


Now, as there was a difference in metrics I decided to run two tests, one with removed if/else other keeping that.



It is very hard to say what is the best option as the results are same (and we shall take at least 20 probes of same to be able to say something about true results (and as one run takes 3 minutes - I'm not going to bother with it).

the file is here: https://github.com/profesor79/FizzBuzz/blob/main/02ThomasLevesque.cs

Now, there is a solution that is saying: "Unrolled for maximum efficiency. This program can outfizzbuzz all others." 

As we can see on the snippet - it is just funny to code like that, metrics going orange as we have a lot of lines of code here. Now this is the one solution that can't be tested using a single input as it runs by 15, so most common requirement to work from 1..100 is not fulfilled.



Let's look for another pearl - time for a LINQ guru.

What do you think? - it looks complex and hard to understand in a first view, and Halstead volume metrics is the highest atm. What that means that there is a lot of operands, operators and operations all together.
the benchmarked code is here: https://github.com/profesor79/FizzBuzz/blob/main/_03Xela.cs

I think this is the slowest  solution,     


Now we are going to see something that has very low metrics:

Looks good? There is a bit of magic to shift the values inside the array (so the 3 us multiplied by 2 to get empty space and 5 has added 1 to shift it as well) file here: https://github.com/profesor79/FizzBuzz/blob/main/pythonic.cs


Now can we do it without IF statement, in a way just do it?

as we can see on the listing above it is a bit of preparation to get it, and this piece of code get's the best metrics (again some stuff is outside the function so it is a kind of cheating)   



We did it without if with a price of CPU ticks (there probably will be a person that will do it faster), but this was just to prove the concept.

Last one uses C# 8 pattern matching, and it is just as easy to understand and to extend - have a look:

That's cool - isn't it?
Now look at this:

I have got no questions about that!! This is insane, PatternMatching is treated as one liner, easy to follow and is that fast?

THIS IS INSANE!!!
The best results, the best metrics, the best ability to extend by adding pattern matching to C# :D





Comments

Popular posts from this blog

when a new guy joins a team...

are YOU a garbage collector?