Skip to main content

FizzBuzz - part 2

 Lead Developer FizzBuzz

In part 1 we went over a simple implementation of FizzBuzz.  Now we are going to advance to what I expect a lead developer would create.

Job titles are tricky things.  I am not equating this with a job title, what I mean by a lead developer is that they could help a more junior developer develop a more complete solution.

Unit Tests

The first change is the addition of unit tests.  Developers will develop structurally different code if they are required to develop unit tests.  In order to test code correctly, you have to be able to expose the different facets of your creation.  This allows unit tests to be short and robust.

package dev.boundary.waters.FizzBuzz;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class FizzBuzzTest {

@Test
void test1() {
FizzBuzz fb = new FizzBuzz();
assertEquals("1", fb.process(1), "failed for 1");
}

@Test
void test3() {
FizzBuzz fb = new FizzBuzz();
assertEquals("Fizz", fb.process(3), "failed for 3");
}

@Test
void test5() {
FizzBuzz fb = new FizzBuzz();
assertEquals("Buzz", fb.process(5), "failed for 5");
}

@Test
void test15() {
FizzBuzz fb = new FizzBuzz();
assertEquals("FizzBuzz", fb.process(15), "failed for 15");
}

@Test
void test0() {
FizzBuzz fb = new FizzBuzz();
try {
fb.process(0);
} catch (Exception e) {
return; // success
}
fail("Didn't get an exception for less than 1");
}

@Test
void test101() {
FizzBuzz fb = new FizzBuzz();
try {
fb.process(101);
} catch (Exception e) {
return; // success
}
fail("Didn't get an exception for less than 1");
}
@Test
void testEmpty() {
FizzBuzz fb = new FizzBuzz();
try {
fb.fizzBuzz(0, 0, System.out::println);
} catch (Exception e) {
return; // success
}
fail("Didn't get an exception for empty range");
}
@Test
void testOutOfOrder() {
FizzBuzz fb = new FizzBuzz();
try {
fb.fizzBuzz(101, 1, System.out::println);
} catch (Exception e) {
return; // success
}
fail("Didn't get an exception for empty range");
}

}

Factor out the hard coded input

The fizzBuzz method now takes the input parameters and output destination as parameters, allowing for tests as well as preparing for future changes:
  public void fizzBuzz(int start, int finish
                             Consumer<String> func) {
if(start >= finish) {
throw new IllegalArgumentException("start " 
                          + start + " must be less than finish " 
                          + finish);
}
IntStream.range(start, finish).mapToObj(
                  n -> process(n)).forEach(
                  s -> func.accept (s));
}

Optional: convert to Java streams

The advantage of changing to Java streams is that you can't put an if, break, return or other flow control statement into the stream by mistake.  You also set yourself up for the ability to parallel process with a thread pool, etc.

The downside to this change is that it is quite a bit harder to debug (with a debugger) and it may not be common in a lot of teams to use Java streams.  Both are valid reasons to not make this change.

Comments

Popular posts from this blog

Spring Boot native builds when internet downloads are blocked made simple

 No direct access to the internet If you work at a company that controls their software bill of materials, it's quite common to be blocked from directly downloading from: Maven Central Docker hub GitHub (the public parts) Getting the bits Maven Maven is first, because without it, you won't be able to compile your Spring Boot application, let alone move on to turning it into a native docker image. I will be showing changes need to work with artifactory, but you should be able to adapt it to other mirror solutions.  repositories {   maven {     name = "central"     url = "https://artifactory.example.com/central"     credentials {       username = "${project.ext.properties.artifactory_username}"       password = "${project.ext.properties.artifactory_apikey}"     }   } } With this configuration change, you should be able to download your plugins and dependencies, allowing you to compile and ...

Kotlin Notebook when you're blocked from Maven Central

 TLDR; If you are blocked getting to maven central when first using Kotlin Notebooks because of company firewalls, you can use a tool like Fiddler Tool to redirect to a different network location. Kotlin Notebooks Kotlin Notebooks are a JDK based environment that brings the Python based Jupyter Notebooks  expressiveness to IntelliJ. From the blog post announcing the plugin, it looks like this: At home, the installation of jar files looked like this: I played around with it at home, but I couldn't use it at work.  Many companies, mine included, do not allow software components to be used when downloaded directly from the internet. In my companies case, we use a product called Artifactory, which allows you to mirror the content from Maven Central while still applying policies like CVE scanning, tracking, etc. The way it should work IntelliJ, as one of the leading IDE's, generally supports this quite well.  In fact, there is a whole setting page dedicated to dealing wi...

BSOD Unexpected Kernel Mode Trap

 BSOD In Windows, the blue screen of death is what is shown to users when the operating system encounters something it wasn't expecting so bad that it decides to just quit rather than attempting to continue operating. This isn't a single task that has failed, like Windows Explorer.  If that dies you lose the ability to interact with windows, but you can restart the process using keyboard shortcuts. The behavior My Windows 11 desktop had always been a little flaky, occasionally hanging every few weeks. It was annoying, but not enough to actually go through the effort to diagnose or re-install everything.  But after a recent patch Tuesday, the behavior had changed.  Rather than hanging, it started displaying a BSOD, rebooting and then running just fine. I also have three different accounts on my desktop: My standard, low permission account I use for day to day activities and development, games, etc. An Administrator account that is linked to my Microsoft account An Adm...