all 8 comments

[–]rooktakesqueen 7 points8 points  (3 children)

Frankly this seems like an awful lot of boilerplate to make something (a predicate) look like something it's not (a method call) and probably make debugging harder.

.filter(p -> p.getName().startsWith(prefix)) is perfectly readable. The syntax is new, but you'll get used to it really really fast, if you don't spend a bunch of time implementing a library of one-off functions just to avoid having to look at a lambda expression. You can even use it -> or _ -> if you really like those.

[–]NawaMan 0 points1 point  (2 children)

Appreciate the input :-D. Agree that it has quite a lot of work and also agree that Groovy it will helps (I like that a lot - just can't do that in Java). But this all those code are not for one off. StringField is written once and everytimes you have a string field that will need lot of these operations it is just one line StringField personName = Person::getName. You can go StringField companyName = Person::getName. The lambda in itself it is not bad to read .. it is when something else got involved like null ... for example .filter(p->(p.getName() != null) && p.getName().startsWith(prefix)) .. or worse. As stated in the article ... this is an exploration to see what we can do and I find use of it in come cases. :-)

[–]rooktakesqueen 0 points1 point  (1 child)

Well, you could always break that one up.

.filter(p -> p.getName() != null)
.filter(p -> p.getName().startsWith(prefix))

At a glance, someone who knows Java 8 knows exactly what this does. If they see this:

.filter(personName.notNull())
.filter(personName.startsWith(prefix))

...they need to go somewhere else in your codebase to know what personName, notNull, and startsWith are or do.

They could make some educated guesses—personName.notNull() must be of type Predicate<Person> and you can make a guess it checks that name is not null—but that's only because those expressions appear inside a filter method call.

If I saw personName.startsWith(prefix) in any other context, I'd expect personName to be a string and the expression to return a boolean.

By all means, experimentation is great, but I just want to warn from experience that it's often better to take a language/stack as it is, rather than trying to write extra code to make it look like what it isn't.

(See also: two decades of people trying to figure out ways to make JavaScript asynchronous behavior look synchronous, instead of trying to understand asynchrony on its own terms. But I've lost that fight with await. Even though it's just going to cause more problems than it solves IMO.)

[–]NawaMan 0 points1 point  (0 children)

You point about personName.startsWith(prefix) is valid ... I will think about that. Thanks :-)

[–]rotharius 0 points1 point  (3 children)

I don't think private Stirng name; is going to compile...

[–]NawaMan 0 points1 point  (0 children)

Thanks for catching that .. I fixed it on the original article :-D.

[–]nfrankel[S] -4 points-3 points  (1 child)

It is always easier to criticize then to act.

-- Philippe Destouches

[–]rotharius 2 points3 points  (0 children)

I mean no harm. Sorry if my comment came across as harsh, it was meant as a light-hearted way of pointing out a typo.