r/functionalprogramming • u/raulalexo99 • Feb 13 '23
Java [Begginer in FP] Can you help me make this method more Functional and less Imperative?
I am using the OOP builder pattern (Java). The client code looks like this:
private Label label() {
return LabelBuilder.withText("Select your language")
.font("Arial", PLAIN, 38)
.alignment(CENTER)
.maxSize(500, 300)
.build();
}It looks pretty nice. But still, inside the LabelBuilder class the code looks way more imperative and with duplication. The following is the terminal operation to actually build a Label:
public Label build() {
var label = new Label(text);
if (font != null) label.setFont(font);
if (alignment != null) label.setAlignment(alignment);
if (maximumSize != null) label.setMaximumSize(maximumSize);
return label;
}As you can see, there are three null checks on the left, and three consumer operations on the right.
I could not find an OOP way to reduce the duplication. Maybe there is a functional way to reduce the three if statements to only one? The same with the label.setFoo() methods?
4
u/tobega Feb 13 '23
First of all, why do the null check? You could just set null in the setter?
Second, you don't want the setters. Never in functional style and usually not in good modern OOP style either. Your object should be immutable unless you absolutely need to change it. And even then you should look for other options.
11
u/[deleted] Feb 13 '23 edited Feb 13 '23
The builder pattern isn't really functional because it usually involves updating mutable state in the builder class before building it. To complicate things Java isn't really the best language for functional programming, but if you are going to use Java I would go with the "wither" pattern. It's similar to the builder pattern except instead of updating mutable state, you define functions that return a new instance of the class every time you update a property. It would look something like this. Like many other things in Java there's a good amount of boiler plate.
Lombok has the @With annotation to remove a lot of the boiler plate I wrote above. You should also know this a is a special case of a general functional program concept called lensing. Scala has a lensing library called Monocle that makes all of this a lot nicer.
https://www.optics.dev/Monocle/
6
u/[deleted] Feb 13 '23
Java is a really bad place to learn FP. 99% of language is fighting against you. I just looked up Functional composition in Java and it looks horrible. Look at an actual functional language Haskell, OCaml. Typescript with fp-ts is also a decent option.
3
u/hvihvi Feb 14 '23
To remove mutability, instead of building an unfinished instance by modifying it over time (via setters), you create it in its final state. To do so you map all inputs to their final value before creating your Label. Like someone said, a constructor would get the job done.
You could null-check before building, and use distinct constructors. Or be explicit about default values instead of being implicit, for example: