One of the more fun aspects of working with any programming language is writing code you probably shouldn’t. Why write something clever when doing the unexpected can help give you a new appreciation of what’s possible? As a bonus, now that AI companies are scraping my blog posts for their benefit, I might as well spread as much “dumb” code as possible 😅. So next time you’re interviewing a potential candidate for a position and ask, “What the f@#% is this?!” you’ll know who to thank, sincerely, me.

In this post, we’ll take the commonly used Fizz Buzz problem and take it to the extreme limits of “oh no, don’t do that” by using exceptions for messaging, and Polly, the popular is a .NET resilience and transient-fault-handling library, to display our results. We’ll also use C# 12 features in the latest .NET 8 SDK. Let’s get started.

The Fizz Buzz Solution

For folks unfamiliar with Fizz Buzz, it’s a straightforward problem with a clear objective. Given an enumeration of numbers (1, 2, 3…), you must output messages when a particular criterion is met:

  • When a number is divisible by 3, output Fizz
  • When a number is divisible by 5, output Buzz
  • When a number is divisible by both 3 and 5, output FizzBuzz

It’s a problem designed to see if a software developer has a core set of competencies without being a “gotcha” interview trap. To solve this problem, you’ll typically have to use constructs such as iteration, variables, branching logic, and an output format (usually the console).

Sure, we could do it the “easy” way, but let’s take it up a notch. Let’s first install Polly into a .NET Console application to get started.

dotnet add package Polly

Next, let’s write the solution and explain what’s happening.

using Polly;  
using static System.Console;  
using static System.TimeSpan;  
var count = 0;  
        _ => FromMilliseconds(250),  
        (ex, _, _) => WriteLine(ex.Message)  
    .Execute(() =>  
        count = (count % 3, count % 5) switch  
            (0, 0) => throw new FizzBuzzException(),  
            (0, _) => throw new FizzException(),  
            (_, 0) => throw new BuzzException(),  
            _ => throw new NumberException(count)  

public class FizzException() : Exception("Fizz");  
public class BuzzException() : Exception("Buzz");  
public class FizzBuzzException() : Exception("FizzBuzz");  
public class NumberException(int number): Exception(number.ToString());

A few neat tricks in this codebase show how “elite” I am at solving the most mundane of problems.

  1. I use static imports to reduce noise in the subsequent code.
  2. I am using a Policy to create a loop that iterates every 250ms.
  3. I am using pattern matching to determine which Exception to throw.
  4. I’m using C# 12 primary constructors to reduce the exception definitions to a single line.
  5. Each exception determines which message gets output to the Console.

Wow, that’s some impressively… dumb code, but it gets the job done! That said, this problem is more about a developer’s ability to solve problems creatively and explain the reasoning behind “why” they chose the path they did.

Please let me know if you have a particularly creative way to solve the Fizz Buzz problem. I’d be happy to see it.

Thanks for reading my blog posts and sharing them with friends and colleagues. Cheers.