Skip to content

Latest commit

 

History

History
120 lines (105 loc) · 2.94 KB

step-by-step.md

File metadata and controls

120 lines (105 loc) · 2.94 KB
  • Start by inverting condition Invert if condition

  • Apply the same safe refactoring for the other if on input Invert if condition

  • Our code now looks like :

public static String convert(Integer input) throws OutOfRangeException {
    if (input <= 0) {
        throw new OutOfRangeException();
    }
    if (input > 100) {
        throw new OutOfRangeException();
    } else {
        if (input % 3 == 0 && input % 5 == 0) {
            return "FizzBuzz";
        }
        if (input % 3 == 0) {
            return "Fizz";
        }
        if (input % 5 == 0) {
            return "Buzz";
        }
        return input.toString();
    }
}
  • We can remove the redundant else Remove redundant else

  • We group the guard in the same condition

public static String convert(Integer input) throws OutOfRangeException {
    if (input <= 0 || input > 100) {
        throw new OutOfRangeException();
    }
    if (input % 3 == 0 && input % 5 == 0) {
        return "FizzBuzz";
    }
    if (input % 3 == 0) {
        return "Fizz";
    }
    if (input % 5 == 0) {
        return "Buzz";
    }
    return input.toString();
}

Congrats, we now have only one level of indentation. We still have space for improvement...

public static String convert(Integer input) throws OutOfRangeException {
    // Encapsulate the condition
    if (input <= 0 || input > 100) {
        throw new OutOfRangeException();
    }
    // Avoid magic numbers
    if (input % 3 == 0 && input % 5 == 0) {
        return "FizzBuzz";
    }
    // Modulo repeated everywhere
    if (input % 3 == 0) {
        return "Fizz";
    }
    if (input % 5 == 0) {
        return "Buzz";
    }
    return input.toString();
}
  • Here is an "improved" version of it
public class FizzBuzz {
    public static final int MIN = 0;
    public static final int MAX = 100;
    public static final int FIZZ = 3;
    public static final int BUZZ = 5;
    public static final int FIZZBUZZ = 15;

    private FizzBuzz() {
    }

    public static String convert(Integer input) throws OutOfRangeException {
        if (isOutOfRange(input)) {
            throw new OutOfRangeException();
        }
        return convertSafely(input);
    }

    private static String convertSafely(Integer input) {
        if (is(FIZZBUZZ, input)) {
            return "FizzBuzz";
        }
        if (is(FIZZ, input)) {
            return "Fizz";
        }
        if (is(BUZZ, input)) {
            return "Buzz";
        }
        return input.toString();
    }

    private static boolean is(Integer divisor, Integer input) {
        return input % divisor == 0;
    }

    private static boolean isOutOfRange(Integer input) {
        return input <= MIN || input > MAX;
    }
}