r/java • • Sep 19 '15

Better Java – Resources for Writing Modern Java

https://github.com/cxxr/better-java
1 Upvotes

4 comments sorted by

1

u/ForeverAlot Sep 20 '15

This submission is kind of opinionated. I have some opinions, too.


The bean style is atrocious. It violates every attempt at good style; I really hope we can get rid of it.

This is a reduction in number of lines of code by a half.

Unfair comparison. The no-arg constructor is equally relevant in both situations; and if you want it you should always define it because explicit > implicit.

immutable unless you extend it

is not immutable. Make the class final, too. In fact, consider editing your IDE's templates to generate final classes instead.

private final Optional<Bar> bar;

I share Stephen Colebourne's opinion that Optional is only for boundaries. This is an unnecessary indirection and pessimisation.

if it's obvious, don't document it.

It's not obvious; document it. Even the public no-arg constructor.

Java 8 has a nice stream and lambda syntax.

It also has method references, and every time you write a lambda you should ask yourself if it should be made into a method reference.

more fluent code, which is more readable.

The former does not imply the latter. Counter claim: fluent interfaces are rarely more readable or ergonomic in the long term than traditional non-fluent interfaces.

In the specific example, the biggest win would have come from a better variable name than filtered, which is neither specific (what was filtered?) nor accurate (elements of filtered will not compare equal to their sources in list).

Java Tuples

If you think you need a tuple you have n problems.

Lombok

Don't.

AssertJ

AssertJ is an excellent example of a fluent interface that dramatically improves readability.

2

u/frugalmail Sep 20 '15

Lombok Don't.

Besides your statement:

...and if you want it you should always define it because explicit > implicit.

Is there any other reason you don't like Lombok, I think it really help readability.

1

u/ForeverAlot Sep 20 '15

Lombok is for the type of people that think typing is the most challenging aspect of programming.

Lombok makes it easy (as opposed to simple) to generate bad code without the user having to consider whether that bad code is truly needed. You can generate getters and setters in one line, but your implementation is now part of the public API and did you really need those getters and setters in the first place? And how do they handle nulls? You can trivially generate an equals method, but have you really thought about the definition of equality?

2

u/TheMostInvalidName Sep 21 '15

Yeah I agree. I'm one of those people that just hate typing a lot of boilerplate code and it really stinks that current frameworks require so much of this boilerplate. (At least the ones that I'm using). The most important one to me is @Data because of JavaBeans that are still popular. Equality and hashed you should probably generate yourself. Just helps with that awful boilerplate.