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.

Popular Pragmatic topics Top

iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
brunogirin
When installing Cards as an editable package, I get the following error: ERROR: File “setup.py” not found. Directory cannot be installe...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
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

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
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
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New

Latest in PragProg

View all threads ❯