Once upon a time ... The story behind “Yet another FizzBuzz coding exercise”

Jun 7, 2012 at 4:04 AM

Intro. Once upon a time ... The story behind “Yet another FizzBuzz coding exercise”

I started this project as part of an interview process with a top notch consulting company; I told one of my friends that I was going to start working on this and he said “Borrrinnnngggg!!!!, FizzBuzz is something they taught me in high school, it’s a no brainer”.

FizzBuzz rules are simple:

  • Print all numbers from A to Z,
  • if a number is divisible by X then print ABC
  • if a number is divisible by Y then print XYZ
  • if a number is divisible by X and Y print ABCXYZ

Easy a loop, modulus and a couple of ifs will do the trick, but this is not about coding FizzBuzz it’s about me:

  • How is my coding style? is my code clean, commented, following naming conventions, object oriented vs. spaghetti code?
  • Do I know the features of the programming language that I’m using: generics, interfaces, overloads, config files, LINQ lambda expressions
  • I’m using the latest tools? IDE, source control
  • Am I following a design pattern, a development methodology, architecting my tiers properly

Before I get too personal and start talking about myself, let’s go ahead and start recalling the story behind “Yet another FizzBuzz coding exercise”

Chapter 1. It all starts with a Big Bang

I’m doing this exercise on my 2 year “brand new” desktop replacement laptop that was put to the test/approved by all my children playing Angry Birds.

But since I hadn’t used for any “real work” I had to get the tools I need, I got VS Studio 11 Beta, I heard of VS 2012 but not 2011 beta, anyways I downloaded and installed, big shocker compared to VS 2010 that I’ve used for a while, more grayish that expected, but some nice features as well, install process didn’t took that long or I got used to it after installing different instances of VS during my career or the extra hardware I got on the laptop finally paid off.

Finally got my tools in place: VS 11 Beta, high-end laptop and my cup of extra dark, no sugar, and no milk Colombian coffee.

 Chapter 2. Round 1 Get to know your opponent

First I threw a few easy punches: setup a solution file with the following:

  • A Class Library project

This was the easiest part FizzBuzz logic isn’t that complex and I got the happy path working pretty quickly.

  • A Console app

At this early stage I decided to leverage this one for later, and just run simple FizzBuzz samples.

And of course my companion for a long time

  • A Test project to hold my unit testing.

I’ve been asked recently about my TDD exposure, and honestly I haven’t worked on a project that unit testing was a must or that adhering to TDD principles was enforced, all what my boss cared about was my timesheet and why it was taking me longer to code whatever he estimated with his crystal ball.

As a seasoned developer I’ve been testing my code even before TDD was mainstream, you need to make sure that whatever you’re coding it’s crunching the numbers right. TDD, unit testing frameworks, mocks and whatnot tools are just to make your testing easier not to avoid it.

I set up basic test cases and had my Unit Tests working, thank you VS for make it to seamless.

Chapter 3. Round 2 Go to your corner and work on your strategy

I made it coach! I came of 1st round untouched, what’s your advice?

I got the basic stuff working, so it was time to start looking at the fine print. I’ve been in some many projects where the fine print gets everything falling apart, you know the kind of stuff that starts like: don’t worry now it’s just a simple calculation that you’ll have to code and we’ll figure out later what else we need. Yeah sure! Snowflakes if they keep piling up one day they may be part of an avalanche.

But let’s back to our main story, I did some refactoring on the main FizzBuzz functions, and added extra unit test this time even with null instances to validate for expected exceptions.

Chapter 4. Round 3 KO or go home

Finally I got to the last round: it was now or never. Even though I got it working and cover all the requirements, still I felt that I need the extra mile push and I went for it: added extra overloads to handle different type of output destinations: Console, TraceListeners, ICollection<string>, IO.TextWriters, IO.Streams and even a custom Interface to fill in the blanks for whatever wildest dream someone will have, and all nicely covered by corresponding unit tests.

Once I had everything running nice and tight I ended up icing the cake with something I remember from back in the day: an old style DOS command menu for running FizzBuzz test from the console app allowing the user to run the console and play over and over again until hitting the quit option.

Conclusion. In the end all what matters is the fun you had

Well after spending a couple of days playing with FizzBuzz, all I can share is that I’ve enjoyed it and didn’t got empty handed since I learned a few tricks during the process, and you never know one of those days I might come back an revisit/refactor FizzBuzz and take it to the next level

Oh! I almost forgot I like to thank you FizzBuzz for cutting me some slack with my wife for not mowing the lawn on the nice Texas summer heat.

Gracias Totales! Chau!

Cesar H