dtonhofer

dtonhofer

Functional Programming in Java, Second Edition: p.103 `FluentMailer`: Suggesting to rename `block` to `configurator`

On page 103, the code for FluentMailer provides a static method which is basically a service that (opaquely) instantiates a FluentMailer, gives the instance to a Consumer<FluentMailer> passed in by the caller in the form of a lambda, which is supposed to configure it, and then performs whatever operations are necessary to “send an e-mail” based on the configure FluentMailer.

The Consumer<FluentMailer> parameter is called block which is nondescript and maybe confusing. I suggest to call it configurator because that is what it is meant to do.

We also read:

Rather than creating an instance, users will now invoke send() and pass a block of code. The send() method will create an instance, yield [?] it to the block, and, upon return, complete any required validations and perform its final send operations.

This may feel a bit roundabout, but we removed the smells we discussed earlier. The object’s scope is confined within the block, and once we return from the send() method, the reference is gone. We can also benefit from the fluent method chaining within the block, without the sour new keyword sticking out. Let’s use this new API in an example.

Suggesting:

Rather than create an instance to manipulate, client code will invoke the static send() method, passing an instance of Consumer. send() will create a FluentMailer instance, call the Consumer on that instance, thus performing client-controlled configuration, then complete any required validations and finally perform its final send operations.

This may feel a bit roundabout, but we removed the smells we discussed earlier. The ‘FluentMailer’’s instance scope is confined to within send(), there is no new call to be done anymore and we can benefit from the fluent method chaining to do FluentMailer configuration within the Consumer. Let’s use this new API in an example.

Where Next?

Popular Pragmatic Bookshelf topics Top

sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=&gt; (create-...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
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
digitalbias
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64) If you follow the defau...
New
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New

Other popular topics Top

AstonJ
Or looking forward to? :nerd_face:
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1139 25582 756
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: