Uncharted Waters

Jun 18 2019   10:11AM GMT

In Defense of FizzBuzz

Matt Heusser Matt Heusser Profile: Matt Heusser

Tags:
HR
Interviews

In Defense of FizzBuzzFizzBuzz is a classic programming challenge, or “Kata“, popularized by Jeff Atwood, that requires the test-taker to demonstrate simple concepts like looping and selection. If you are active in the software world, you may know that it was popular at one time and has faced increasing criticism. One explanation for the decline of FizzBuzz is simple evolution. People just learned better interview techniques. Where Fizzbuzz was once the best we knew, today we know better.

I don’t buy it.

The typical programming interview is repeating a list of technologies and yes or no answers. Some teams use a contrived google style tech interview where  navigate a binary tree.

A few companies do better, asking technical staff to pair, do TDD, or demonstrate expertise in other ways.

Here’s a way to do it with FizzBuzz.

FizzBuzz Defined

“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.” – C2 Wiki

We worked this problem last week at Bagels and Bums with David Hoppe and Othelle McAlister. After initially solving it, we talked about what coding styles are better. For example, here’s two pieces of code:

if (val %% 3==0) {
  result = "fizz";
}

Or this?

if (val %%3 == 0)
  return = "fizz";

In C, C++, and C#, if the inside of the if statement is just one line of code, then you don’t need need curly braces. I argued that less lines of code means tighter, and easier to read.

So David solved FizzBuzz in one line.

David Hoppe's Fizzbuz

David was making the point that “shorter” does not always mean more readable, and it certainly does not mean easy to maintain.

After that we talked about the “two” biggest problems in Computer Science: Naming, caching, and off-by-one errors. The for loop for fizzbuzz is “(int i=1; i<=100; i++).”  That little “equal to” before the number 100 is the difference between a program that is correct, and one that stops at ninety-nine. The way to make sure the program does the right number of iterations is to check. In a simple program you can look at it. In a more complex program, that loop will be hidden away, best to drive it out with TDD.

That brought us to design and testability.

FizzBuzz and Testability

When most people write fizzbuzz, they do the whole thing in one main routine. My implementation was a little different. Here’s the function signature:

string FizzCalc(int iNum);

string [] FizzBuzz(int iMax);

avoid main();

FizzCalc calculates the string for a given number; FizzBuzz takes in a number and returns a string array of all FizzBuzz numbers from 1 to iMax. By separating the concerns of the programs, I make it possible to test-drive, which I could do in a separate blog post.

For now, here’s the implementation.

Matt's FizzBuzz, Part IThat led us to a discussion about naming, one of the two, er, um, three most difficult problems in Computer Science. I write my code tight enough that there is no need for the “hinting” of the type in the variable name. In general, we agreed that  shorter variable names are more appropriate when the code is localized, such as a single loop index. David pointed out the importance of iterators as a construct, over traditional “for” loops.

Yes, we did pull all that out of FizzBuzz. We did it by talking about the code, instead of trying to make it about who was smarter.

Matt's FizzBuzz2

Start with FizzBuzz. Then keep going.

What started as a simple problem, for us, turned in a conversation about ideals in programming. Instead of a “right” and  “wrong” answer, we talked about what kind of code is better or worse. Adding to that a discussion of  simplicity,  expressiveness, ease to read and maintenance was just the tip of the iceberg.

What would happen if you did this for a five different problems? Or twenty? Or fifty? That is the stuff that exceptional programming is made of.

If you don’t do deliberate practice in your work, I dare suggest it may be time to start.

When you start, do it with a partner.

 

 

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: