News for hackers. Stuff that matters.
Re:Java Verbal Expressions by noema on Wednesday November 25, 2020 @01:02PM (#25212129)
The main intent of Builder isn't performance, but to avoid a combinatorial explosion of constructors for every possible set of parameters.
Re:Java Verbal Expressions by lalaithion on Wednesday November 25, 2020 @02:14PM (#25213062)
So why have constructors for every possible set of parameters?

Why

    VerbalExpression.regex()
        .startOfLine().then("http").maybe("s")
        .then("://")
 .maybe("www.").anythingBut(" ")
 .endOfLine()
 .build();
Instead of

    new VerbalExpression()
        .startOfLine().then("http").maybe("s")
        .then("://")
 .maybe("www.").anythingBut(" ")
 .endOfLine();
Re:Java Verbal Expressions by szatkus on Wednesday November 25, 2020 @03:44PM (#25214105)
Both are equally readable to me, but with the builder pattern you have an ability to fork a builder. Cloning objects in Java could be messy.
Re:Java Verbal Expressions by stickfigure on Thursday November 26, 2020 @12:59AM (#25217292)
With immutable objects, every step in the fluent chain of calls is an independent fork of the full object state. There's no need to use java clone(); each method calls a private constructor that passes the object state (slightly altered, of course).

java.lang.String works exactly this way. You're already used to the pattern.

Re:Java Verbal Expressions by tpxl on Wednesday November 25, 2020 @06:28PM (#25215453)
The builder allows you to have immutable objects. While I dislike builders and seldom use them, I dislike mutable objects even more.
Re:Java Verbal Expressions by Tainnor on Wednesday November 25, 2020 @09:28PM (#25216322)
In the parent's suggestion, you still have immutable objects. "VerbalExpression()" is a valid regex, namely the empty one. Every subsequently called method concatenates some new regex onto the receiver and thus builds a new regex (since regular expressions are closed under concatenation).

Builders are used in Java* when you have an object which is invalid without passing in a bunch of parameters, but you don't want to have to remember the order of the parameters. But this is not one of these cases.

There is one drawback, however: you do have to compile the regex into a FSA at some point and it wouldn't be good to do that for every intermediate regex. So I assume that the compilation happens in the "build" step. They could have just called it "compile", though.

* named parameters in constructors seem a better alternative in every language that supports them, e.g. Kotlin

Re:Java Verbal Expressions by stickfigure on Thursday November 26, 2020 @12:45AM (#25217222)
Furthermore, with immutable objects there's no need to to have any public constructors at all.

`REGEX.startOfLine().then("http")...`

Since the start object is immutable, there's no reason to create new ones. Here's an example of this pattern applied to HTTP (my project):

https://github.com/stickfigure/hattery

Avoiding repeated Pattern.compile() is a good point though.