Article Review: FizzBuzz Refactoring Challenge with Open/Closed Principle

Ismaeel Shuaib
Facebook Developer Circle: Accra
3 min readSep 6, 2020

In this week’s episode of TTD, we have Oliver Mensah reviewing an article he posted on Dev.to a couple of months ago. The article was about FizzBuzz Refactoring Challenge with the Open/Closed Principle. Oliver explored instances where you are required to add more conditions to the “fizzbuzz” rule and how to meet such requirements using the Open/Closed Principle.

Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word “fizz”, and any number divisible by five with the word “buzz”.

It is a simple but important programming task, used in software developer job interviews, to determine whether the candidate can actually write code. Software development is not always about writing code. The essence is always to solve a problem. Writing code is just one of many tools used to achieve a bigger goal. Most employers regard success in “fizzbuzz” problem as a quick way for developers to make a great impression thereby keeping them in the interview process.

…this simple exercise is still a quick way to get a first impression.

_Daniel Bunte: Why I’m still using “Fizz Buzz” to hire Software-Developers

The concept and interpretation of the “fizzbuzz”

Write a program that prints numbers from 1 to 100. If a number is a multiple of 3, print “Fizz” instead of the number, and if it’s a multiple of 5, print “Buzz”. For numbers that are multiples of both 3 and 5, print “FizzBuzz”.

The flow chart below should help

fizzbuzz flow chart
code representing the flowchart

Concept and interpretation of Open/Closed Principle

The OCP states that you should be able to extend a class behavior without modifying it. Meaning, a unit of code can be considered open for an extension when its behavior can be easily changed without modifying it. The fact that no actual modification is needed to change the behavior of a unit of code makes it “closed” for modification.

“In the FizzBuzz code shown previously, the FizzBuzz class should be able to add more requirements with minimal or no changes to the codebase. To do this, we need to be able to identify what is happening in each condition that determines a strategy and remove what’s common into a specific abstraction such as interface, abstract class, and class.”

Here’s Oliver’s video, where he talked extensively about the Open/Closed Principle.

Further Reading

Check Oliver’s earlier post on Dev.to.

And here’s a GitHub repo on the code examples.

If you enjoyed this post (and we know you did :) ), give us a clap and like our Facebook page.

NB: If you would like to participate/share a tech knowledge with us, please get in touch through this form.

#TheTechDemystify is in every Sunday at 7 pm on Facebook Group, make sure to tune in and enjoy every reverting conversation as well as tutorial sessions to help you grow in tech.

--

--