How to Skin a Cat: FizzBuzz in Five Languages

Eddie Prislac
Quick Code
Published in
13 min readFeb 22, 2019
SKINAWHUTNOW?

— UPDATE: Due to some helpful suggestions in the comments, I’ve revised this article from the original version I published, so if you’re seeing this for the second time, don’t be surprised if it reads slightly differently. It’s also why I changed the name from ‘FizzBuzz 5 ways’ , to ‘FizzBuzz in Five Languages’, as there are WAAAAY more code examples than I had, previously. Also, more cat jokes and references, so there’s that.

FizzBuzz is a first-year computing problem many of us have faced. The rules are pretty simple: Given an integer, if the integer is a multiple of three, return ‘Fizz’, if the integer is a multiple of five, return ‘Buzz’, if a multiple of both three and five, return ‘FizzBuzz’, and if it’s not a multiple of either, return an empty string. Most folks’ go-to in this situation is to whip out a simple if/else conditional, but honestly, that’s kid-stuff. To make this a bit more difficult, today, we’ll be trying to accomplish this seemingly easy task using as few conditional statements as possible. To make matters even more difficult, we’re gonna do it in five very different languages (and that’s where this article gets its name… “There’s more than one way to skin a cat… get it?)

Eddie, you’re a madman, that’s crazy talk!
~ you.

We’ll see.

Javascript

The first language we’ll start out with is the one that most beginning developers will be familiar with: Javascript.
Javascript is so ubiquitous in the world of software development these days, that if someone says “I’m a programmer’, it’s more likely they write JS than any other language. The explosion of popularity in what used to be derided by ‘real programmers’ as a ‘scripting language’ can be ascribed to the advent of node.js and its npm ecosystem of libraries. Nowadays, you’d be hard-pressed to find a type of program you couldn’t write in Javascript. While in the past, it was limited to crafting cheesy animations and simple interactivity on web pages, JS can now be utilized to write entire UI frameworks, network servers, real-time chat applications, even automation and/or robotics controllers, such as those created with the Raspberry pi.

Before you say anything, I’ll be deliberately omitting type checking and error handling, just in case any of you are in school and are thinking this article is going to be a good way to cheat.

The most basic code for accomplishing FizzBuzz in JS goes like this:

Simple, right?

First, a few explanations:
1. If you’re just starting out, you may be wondering what that % operator is doing… that doesn’t actually mean ‘percent’. The % is the modulus operator, and modulus is a function we’re going to make a lot of use of in this article. Modulus is the function used when you want to find the remainder of dividing one number by another. In this case, we want to check if the number is cleanly divisible by 3, 5 or both, so we check if the number mod 3 or 5 is equal to zero.

2. No, you’re not seeing triple, those triple-equals signs are the way we check equivalence in JS. Most languages use a double-equals to check equivalence, and a single-equals for variable assignment (ex: let x = "foo"). In JS, the double-equals is used as well, but not as commonly, as the ==operator allows for type coersion; in other words, a string value of '1'would evaluate to be equivalent to a number value of 1. That’s useful if you’re yanking the innerHTML of a tag to compare to a number in your script, but generally ill-advised. The === operator is a strict equivalence comparison, meaning '1' === 1 would evaluate to false.

3. The double-ampersand ( &&)in that third conditional branch is a logical AND , but we won’t be using it again for the remainder of this article, so this will be the last time I mention it.

This code will run reasonably well, and do what it’s supposed to do. However, it can also be written like this:

Fun fact… the guy who introduced me to Immediately Invoked Function Expressions (IIFEs) refers to the parens at the end as ‘donkey balls’. Note how the whole thing is wrapped in parens, then is followed by two empty parens… this makes the JS interpreter view the whole thing as a function that’s immediately executed when loaded, as the name implies.

In this snippet, the code is a lot cleaner-looking. Unfortunately, because we need to instantiate Object twice, it runs about 82% slower than the first example. If it’s slower, why would I even bother showing it to you, you might ask? Well, performance was not the point of this demonstration, but since you asked, consider the results from this test, in which I compare the results of using an Object map vs if/else, vs case/switch (I’m using Chrome — between Chrome, Edge and Opera, which all use the ‘Blink’ rendering engine, you have an overwhelming share of the browser marketplace — The browsers that handle Object mapping slower than conditionals, (Firefox, Safari and IE) when combined, only comprise a little over a quarter of the marketplace, by comparison). In a case where you need to compare static results, tables have turned, and Object mapping is now 80% faster than if/else, and narrowly beats out even switch/case. Why the flip? In this case, if/else is the one which needs to run comparisons every time the operation is run, whereas the object is being used as a binary search tree… it’s only taking the sum and looking up the key in its structure. This is why it’s sometimes much, much faster to utilize this type of structure than iterating over load of conditionals.

— EDIT: I knew when I was writing this that there had to be a better way, and commenter Paul B. was kind enough to point out that there’s a much simpler, and way more elegant solution — string interpolation:

Yes, it’s different than Paul B.’s example in the comments. This is because, while he was quick to point out that most versions of FizzBuzz ask you to return the number instead of Fizz, Buzz or FizzBuzz if the number is neither a multiple of 3 or 5, 1) I’m asking for an empty string, not the number, to set mine apart from most coding tests that use FizzBuzz, and 2) His solution didn’t quite check out, for one reason… He’s using type coercion under the hood to take any number returned from the modulus to be ‘true’ and zero returned by the modulus to eval as ‘false’, which is why his returns are flipped in his example. If you fail to give a param in his version, (or give it a string) the modulus statements will return ‘undefined’… which JS will eval to ‘false’. This means that if you give no param or an incorrect param for the function, it will still give you ‘FizzBuzz’, rather than an empty string, (or, in his case, ‘FizzBuzz, instead of the param itself). That’s me breaking my own rules there, as I said I wasn’t including error checking, again to set it apart from coding tests, but I thought it was worth mentioning. Still, string interpolation was a REALLY good suggestion, as you can see by the js-perf results here: https://jsperf.com/fizzbuzz-if-else-vs-object2

In this example, we’re making use of a string literal to interpolate the results of the modulus calculations directly into the string, thus shaving away the operation down to its barest essentials, and doing away with the Object entirely . This functionality was introduced as part of ES2015, along with the ‘fat-arrow’ function notation I’m using in my examples. True, it goes against the original point of the article by still relying on conditionals, however, this is WAAAY faster and cleaner than both the Object map method and if/else.

Don’t see the conditional? Look a little closer…

Can you spot it? It’s this:

num % 3 === 0 ? 'Fizz' : '';

That’s a ternary operator. Most modern languages support them. What’s going on here is that we’re returning ‘Fizz’ if the condition before the question mark evaluates to ‘true’, else returning an empty string. The question is shorthand for if and the colon for else. This is what’s coined in the biz as a bit of ‘syntactic sugar’.

Python

Going back to the key/value pairs method, let’s try it out in Python:

Ooh, that’s short and sweet!
Python is kinda fun. (Image courtesy of https://www.xkcd.com/353/)

In Python, we see that we can again use a reduce function, and a lot of the parts can almost be recognized from the JS version we looked at previously, even though the syntax has changed greatly. Here, we’re using a lambda list comprehension to reduce the keys to a single string. Also, in Python, whitespace is important… this is why I’ve used four spaces to indent my code instead of two, like in the rest of my examples — in many other languages, whitespace is a nicety, but screw it up, and it’s no big deal. Not so in Python, if your formatting’s off even a little, it causes big problems for your code. (As commentor nleamba pointed out, you can technically use two spaces instead of four for indentation in Python, but the recommended spacing from their own API documentation is four.)

My Python example is less code than the JS version, but I couldn’t quite get away from using that ifstatement, even if it’s in a guard clause, rather than a standard conditional. Also notice the double-equals used to determine equivalence in our modulus checkers — the === does not exist in Python, but that’s the norm, and JS is really the exception, as we’ll see in the rest of our examples…

Paul B’s suggestion got me to thinking… could I use string interpolation to shave some strokes off of my code-golfing game for the rest of my examples? As of Python 3.6, we can, with the f function. However, Python’s ternary is worded as a guard clause, rather than using the x ? y :z format we see in JS, meaning we’re falling back on if/else statements:

Whoa! One line? NICE

Python does give us a shorthand ternary that we can use to avoid if/else as well…:

Shorter, but kinda hard to tell what’s going on in this version.

… however, the shorthand version is not recommended by most Pythonistas, as it is not as ‘pythony’ as most would like. In the shorthand, we’re using tuples (a Python datatype for defining pairs of vars) to define our results, and placing the condition in brackets after the return values. It’s kind of like Shrödinger’s cat… the tuple is your return and exists in a state of both true and false until observed by the conditional behind it.

Hooray! Sciency cat joke!

The ‘false’ return in the ternary shorthand tuple is defined first, which is a bit confusing, as most folks tend to think in terms of ‘true or false’ rather than ‘false or true’. Also, the shorthand version runs the evaluation for both of the values in the tuple, meaning that the condition will be run twice, no matter what. The if/else structured ternary follows standard if/else logic, and does not suffer from the same problem. (refer here for more details)

C++

Remember how, in our first Python example, we had to import the reduce statement? Kinda reminds me of C++… In fact, let’s consider C++:

AAAAAAAAAAIIIIIIIIIIIIGGGGGGGGHHHHHHH!!!!!!

HOKEY-SMOKES, BULLWINKLE, that’s a lot of code!

Wouldn’t it be more thematic to say, ‘HOLY CATS!’?

Of course, I did have to add an entry point (int main() ) with operations to accept input and output the results, (cin and cout) because C++ is a compiled language. JS and Python, are interpreted languages, and can call their functions at runtime without any prep, but as I needed to be able to run the C++ code as a shell script, it was either do it this way, or script in a way to take the number param from the command-line (sorry, I just didn’t feel like it, it’s already a buttload longer without doing that). You’ll notice that even though the syntax looks similar to JS with the parentheses wrapping the params and braces surrounding the methods, we need to do something else in C++, and that’s define the types, not only for each variable we’re using, but also for the return values of each method. We also don’t get quite the easiest way of defining our key/value pair list, which here is called a map. In C++, a vector takes the place of an Array, and we need to set the maximum size of the vector when defining it. This is because C++ forces you to pay a lot closer attention to the memory being used by your program. This is also the reason we need to include the libraries for each of the datatypes we want to use in the program up at the top of the file, because C++ will only include the bare minimum it needs to compile. We also declared a namespace: std. This is because all the libraries we’ve included come from the ‘standard’ library, and if we don’t declare the namespace, we end up writing out std. in front of every declaration of a variable that uses those types. If we’d wanted, we probably could’ve written the same program in C++ using same algorithm as in our first JS example, and it would’ve looked very similar, but still would’ve required a bit more setup and code. JS and Python are both dynamically typed, meaning they infer the type of the variable from the data that’s assigned to the variable. C++ on the other hand, will throw an error if you try to assign a string to a variable which you’ve defined as an int. One final note is that hard as I try, I couldn’t get away from having at least one conditional statement, that ternary operator I mentioned previously in the JS segment.

But can C++ interpolate, Bro?

Short answer? No.

However, I wasn’t about to let that stop me from coming up with something better than that Lovecraftian behemoth I’d originally written, so I did some digging. There are libraries you can add to C++, such as Boost, which include string formatting, but from what I’ve read, including Boost libs tend to slow down your compile-time, and I didn’t want that. I’d rather figure out a way to do it using the standard library, and eventually, I found a way…

Still YUGE by comparison with the others, but way better than the original, right?

In this example, I included the sstreamheader, which allows us to use a basic stringstream to build the string we want to return. Doing this, we can reduce the total lines of code by about half. Still not as pretty as our other languages, but hey, it’s better than it was before.

OK, that’s enough about C++ for now, I need a palate-cleanser. Let’s move on to Clojure.

Clojure

Clojure is a functional language, a dialect of Common Lisp that can be run in the JVM, and can utilize Java libraries. We won’t be getting into any of that Java stuff for now, because we really don’t need it for this example. Be prepared, though, even though Clojure provides us with what was originally the shortest example in this list, and it’s also one of the weirdest-looking:

WHOA.

Yep, excluding the functions’ name, that whole method is only two lines of code, but what the hell is going on?
The first thing is that the entire thing gets wrapped in a set of parentheses, (Clojure is pronounced ‘closure’, after all…), and our function declaration starts off with a defnstatement. Also notice that the param we’ll be inputting ( num ) is wrapped in brackets instead of parens. The other things you should notice is our tblvar is defined with a def statement, and that there are no colons separating the keys from the values. And how about those values? In Clojure, rather that using a % operator, we get the modulus using the mod function, and it comes before the two params… rather that writing num % 3 == 0 , we’re writing mod num 3, which will result in 0. Why are we doing it differently than the other examples? Well, in the list comprehension we’re using to filter down the values, you’ll see that we’re using the comp(compare) function to compare the values to zero. If you squint really hard, that function should remind you a little of the last line of our Python example… the syntax may be different, but the structure of the line is very similar.

In keeping with my other revisions, here’s an example in Clojure using only one line to build a string:

Neat, huh?

Here, we make use of Clojures’ str function to build a string, and the when conditional, which will only return a value when its condition is met.

Ruby

My final examples come from Ruby. Ruby makes me smile, because it allows us to do something really clever. See if you can spot it.

See it yet?

Hey, you may say, that doesn’t look so special… it’s longer than all but the C++ versions of this method, but otherwise doesn’t look all that different from the JS ver… wait what… whaaaaa? What’s that class definition doing there? Where’s your num param? What’s that call to self? WHAT DID YOU DO???!!!

I iz skurd.

Ruby, while also an interpreted, dynamically typed language like JS and Python, and object oriented like C++, allows us a few liberties we can’t really take all that easily in other languages. One of those is the ability to re-open any class and add methods to its definition. In this example, I’ve reopened the Integer class and added the fizzbuzziness method directly to it. This allows the method to be called directly on the number we want to get the ‘fizzbuzziness’ of, rather than calling the method with the number as a param (ex: 5.fizzbuzziness instead of fizzbuzziness(5)). Why in Gods name would you want to reopen a core class of the language and add methods to it? Isn’t that dangerous? Kinda, yeah. You really shouldn’t do it all that often, as methods you add may collide with methods that get added to the standard Ruby library later on, and this could result in massive code-breakages for your application. There’s also the possibility that you could overwrite any method in the class, and end up with something completely nonsensical, like so:

If you do, I don’t know you, you never got this from me.

It’s for this express reason that, even though similar behavior is possible by modifying a class’ prototype in Javascript, they tell you in big, all-uppercase, shouty letters in their API documentation that you should NEVER EVER do it with a standard library class. However… in Ruby, if you have a method like mine that isn’t likely to get added to the standard library, it can be extremely useful as well, as it helps your application adhere to the principle of ‘tell, don’t ask’ in a way that’s more idiomatic to Ruby programming.

We can also do it with string interpolation and ternary operators, like so:

I love Ruby, so much.

And so, oh my droogies, we come to the end of the article. Different programming languages, different methods, same result. A first-year coding problem, beaten within an inch of its life with a meat tenderizer and examined under a microscope. You may not enjoy digging around in different languages to compare how they work as much as I do, but I hope you had as much fun reading this article as I had researching and writing it. Until next time!

--

--

Eddie Prislac
Quick Code

Devil-Dog, Code Warrior, Fevered American Super-Mind. Eddie Prislac is a 12yr+ software development veteran, and Head FNG Wrangler at Vets Who Code.