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...

Active vs. Passive Log4jShell remediation

 Log4jShell  All computer professionals should be aware of the Log4jShell ( CVE-2021-44228 ) and it follow on defects.  There is no shortage of opinions and lessons to be be learned: The difficulty of performing safe interpretation The problems when assumptions are not clearly documented.  I, for one, was completely shocked to find out that a logging system would actually attempt to do variable substitution in an actual message. The difficulty of finding and resolving issues with such a common library that is not provided by an OS package manager. IT'S A LOG4J CHRISTMAS One of my favorite podcasts, Security Now - episode 850 , discussed an analysis by Google regarding the depth of log4j dependencies.  From the show notes : One contributing reason is because Log4j is, more often than not, an indirect dependency. Java libraries are built by writing some code which uses functions from other Java libraries, which are built by writing some code which uses functions f...