https://kotlinlang.org logo
#announcements
Title
# announcements
c

codeslubber

10/25/2017, 10:51 PM
but the Bloch version constructs it in one chain of calls and is thus immutable
p

poohbar

10/26/2017, 12:15 PM
@codeslubber Bloch's builder pattern is nice but is even more verbose. What would I gain? @Paul Woitaschek This is for Java interop. Currently we construct the object by calling empty constructor and then mutating it all over the place by calling setters. That's what the
DogBuilder
would be used for.
c

codeslubber

10/26/2017, 2:20 PM
oh well @poohbar we clearly have nearly opposite orientations, patterns don’t need to justify their existence to me by their ability to decrease LOC counts.. one of the reasons that Bloch was so popular (and why I liked it) is not just the road to immutable construction, but clear readable construction, since Java does not have named arguments, it also increased readability.. when I was using that pattern all the time, btw, I had a bunch of IntelliJ shortcuts that made creating new Builders go really fast..
p

poohbar

10/26/2017, 2:24 PM
I think Bloch's builders are valuable if you need to perform some form of validation before you build the object. I don't need that as the values are validated outside of the context of the class. So it's just a dumb value holder. To me:
Copy code
DogBuilder().age(2).name("Spotty");
is just as readable as:
Copy code
DogBuilder db = new DogBuilder();
db.setAge(2);
db.setName("Spotty");
I value your opinion, what do you think?
p

Paul Woitaschek

10/26/2017, 2:41 PM
There is no need to validate the object before building
You can just do it from the object itself
Copy code
data class Dog(val age: Int) {
      init {
        require(age >= 0) { "Dog isn't born yet" }
      }
}
1
p

poohbar

10/26/2017, 2:48 PM
I have a need to validate the object before construction because I need to collect all invalid input and tell the user what to fix. #realworld
c

codeslubber

10/26/2017, 4:00 PM
@poohbar we also have visited the realworld (unless you were referring to the MTV show there), and validation and object construction are usually kept separate. No one was saying build invalid objects. Certainly FP and favoring immutability would say users providing input and building object graphs that work should be treated as separate problems.
4 Views