HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Ways to improve my coding test FizzBuzz solution for a TDD role?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
waysfizzbuzzimproveroletestforsolutiontddcoding

Problem

I had an interview recently where I was asked to produce the traditional FizzBuzz solution:


Output a list of numbers from 1 to 100.



  • For all multiples of 3 and 5, the number is replaced with "FizzBuzz"



  • For all remaining multiples of 3, the number is replaced with "Fizz"



  • For all remaining multiples of 5, the number is replaced with "Buzz"




My solution was written in Java because of the role, but this was not a requirement. The interviewer was keen to see some evidence of TDD, so in that spirit I went about producing a FizzBuzz unit test:

```
public class FizzBuzzTest {

@Test
public void testReturnsAnArrayOfOneHundred() {
String[] result = FizzBuzz.getResultAsArray();
assertEquals(100, result.length);
}

@Test
public void testPrintsAStringRepresentationOfTheArray() {
String result = FizzBuzz.getResultAsString();
assertNotNull(result);
assertNotSame(0, result.length());
assertEquals("1, 2", result.substring(0, 4));
}

@Test
public void testMultiplesOfThreeAndFivePrintFizzBuzz() {
String[] result = FizzBuzz.getResultAsArray();

// Check all instances of "FizzBuzz" in array
for (int i = 1; i <= 100; i++) {
if ((i % 3) == 0 && (i % 5) == 0) {
assertEquals("FizzBuzz", result[i - 1]);
}
}
}

@Test
public void testMultiplesOfThreeOnlyPrintFizz() {
String[] result = FizzBuzz.getResultAsArray();

// Check all instances of "Fizz" in array
for (int i = 1; i <= 100; i++) {
if ((i % 3) == 0 && !((i % 5) == 0)) {
assertEquals("Fizz", result[i - 1]);
}
}
}

@Test
public void testMultiplesOfFiveOnlyPrintBuzz() {
String[] result = FizzBuzz.getResultAsArray();

// Check all instances of "Buzz" in array
for (int i = 1; i <= 100; i++) {
if ((i % 5) == 0 && !((i % 3) == 0)) {

Solution


  • You can avoid the special case for FizzBuzz



  • No need to hard-code the limits



  • It's not really bad if you have multiple returns in a short (!) method, but it should be avoided if you have an equally readable solution with only one return



I would write it that way:

public class FizzBuzz {

    public static String getResult(int input) {
        String result = "";
        if (input % 3 == 0) {
            result = "Fizz";
        }
        if (input % 5 == 0) {
            result += "Buzz";
        }
        return result.isEmpty() ? "" + input : result;
    }

    public static String buildOutput(int from, int to) {
        StringBuilder output = new StringBuilder();
        for (int i = from; i  0) {
                output.append(", ");
            }
            output.append(getResult(i));
        }
        return output.toString();
    }

    public static final void main(String[] args) {
        System.out.println(buildOutput(1, 100));
    }
}

Code Snippets

public class FizzBuzz {

    public static String getResult(int input) {
        String result = "";
        if (input % 3 == 0) {
            result = "Fizz";
        }
        if (input % 5 == 0) {
            result += "Buzz";
        }
        return result.isEmpty() ? "" + input : result;
    }

    public static String buildOutput(int from, int to) {
        StringBuilder output = new StringBuilder();
        for (int i = from; i <= to; i++) {
            if(output.length() > 0) {
                output.append(", ");
            }
            output.append(getResult(i));
        }
        return output.toString();
    }

    public static final void main(String[] args) {
        System.out.println(buildOutput(1, 100));
    }
}

Context

StackExchange Code Review Q#9749, answer score: 6

Revisions (0)

No revisions yet.