dtonhofer

dtonhofer

Functional Programming in Java, Second Edition: Functional Programming in Java, Second Edition: JUnit code improvements for Chapter 11, pages 189 ff “Refactoring to Rework the Logic”

The usual changes but:

  • The “FirstRepeatedLetter.findIn()” has been improved out of the box. In particular returning ‘\0’ is too C-like and smells like “primitive obsession”. We have null, we should use it. What if the input contains \0 as the matching character?
  • A slightly better code (IMHO) is in FirstRepeatedLetterBetter.findIn()
  • The final refactored code returns not null, but Optional<Character>, which is cleaner.
  • Again, for testing we use a common interface or a wrapper class to avoid having to duplicate testing code.
package chapter11; import org.junit.jupiter.api.Test; import java.util.Optional; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; public class ReworkTheLogicTest { interface FindLetter { Character findIn(String word); } static class FirstRepeatedLetterBefore implements FindLetter { public Character findIn(final String word) { final char[] letters = word.toCharArray(); for (char candidate : letters) { int count = 0; for (char letter : letters) { if (candidate == letter) { count++; } } if (count > 1) { return candidate; } } return null; } } static class FirstRepeatedLetterBetter implements FindLetter { public Character findIn(final String word) { char[] letters = word.toCharArray(); int i = 0; Character found = null; while (i < letters.length && found == null) { // will letter[i] been seen again? char maybe = letters[i]; int j = i + 1; while (j < letters.length && letters[j] != maybe) j++; if (j < letters.length) { found = maybe; } else { i++; } } return found; } } // Does NOT implement "FindLetter", returns Optional<Character> static class FirstRepeatedLetterAfter { public Optional<Character> findIn(final String word) { return Stream.of(word.split("")) .filter(letter -> word.lastIndexOf(letter) > word.indexOf(letter)) .findFirst() .map(letter -> letter.charAt(0)); } } static class FirstRepeatedLetterAfterWrapped implements FindLetter { private final FirstRepeatedLetterAfter finder; public FirstRepeatedLetterAfterWrapped(FirstRepeatedLetterAfter finder) { this.finder = finder; } public Character findIn(final String word) { return finder.findIn(word).orElse(null); } } private static void commonFindFirstRepeatingTests(final FindLetter finder) { assertAll( () -> assertEquals('l', finder.findIn("hello")), () -> assertEquals('h', finder.findIn("hellothere")), () -> assertEquals('a', finder.findIn("magicalguru")), () -> assertEquals('z', finder.findIn("abcdefghijklmnopqrstuvwxyzz")), () -> assertNull(finder.findIn("once")), () -> assertNull(finder.findIn("")) ); } @Test void findFirstRepeatingBefore() { commonFindFirstRepeatingTests(new FirstRepeatedLetterBefore()); } @Test void findFirstRepeatingBetter() { commonFindFirstRepeatingTests(new FirstRepeatedLetterBetter()); } @Test void findFirstRepeatingAfter() { commonFindFirstRepeatingTests(new FirstRepeatedLetterAfterWrapped(new FirstRepeatedLetterAfter())); } } 

Where Next?

Popular Pragmatic Bookshelf topics Top

johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
GilWright
Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
New
PragmaticBookshelf
Tailwind CSS is an exciting new CSS framework that allows you to design your site by composing simple utility classes to create complex e...
New
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File &gt; New Rule: And select Deny, O...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
PragmaticBookshelf
Explore the power of Ash Framework by modeling and building the domain for a real-world web application. Rebecca Le @sevenseacat and ...
New
RobertRichards
Hair Salon Games for Girls Fun Girls Hair Saloon game is mainly developed for kids. This game allows users to select virtual avatars to ...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: