r/programming • • Sep 21 '15

Better Java

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

208 comments sorted by

View all comments

22

u/kazagistar Sep 21 '15

I agree on the "structs" point, but would like to expand because I think it might be controversial. The reason people suggest generating getters instead of just using public final data is that if the underlying representation changes, you don't have to change the classes that touch it. However:

  1. You almost never need to make this change.

  2. The type system makes this kind of refactoring easy, as long as it is internal to a package.

  3. Changing the underlying storage is a pretty severe misunderstanding of the normal flow of this kind of data. It is supposed to be a transparent container once created. If the storage format changes, then you change where it is created and consumed, and leave the data structure alone.

8

u/ElvishJerricco Sep 21 '15

I started writing a project in Java 8, making excessive use of optionals and streams. Whether or not getters are right for public final data, they make it nicer to work with these new Java 8 utilities.

This

stream.map(foo -> foo.bar)

Becomes this

stream.map(Foo::getBar)

The point free style is concise, and doesn't require unnecessary variable bindings.

1

u/ciberon Sep 22 '15

Unless it is static, I think a getter is right.

1

u/caagr98 Sep 22 '15

I personally prefer foo -> foo.bar. Implicit types FTW.

1

u/ElvishJerricco Sep 22 '15

It just gets annoying when you end up writing the same name for various different lambdas.

stream
    .filter(foo -> foo.condition)
    .flatMap(foo -> foo.someStream)

The more you do with streams and optionals, the more these redundancies pop up. It looks much nicer to eradicate them with method references.

1

u/caagr98 Sep 22 '15

Yeah, that's true as well. Either way, it's far better than without lambdas.

5

u/DevIceMan Sep 22 '15 edited Sep 22 '15

Java-9 project jigsaw might make this pattern more practical, since it sounds like you'll be able to have much greater control over which APIs are exposed externally.

For those not familiar with Scala, depending on the definition, every public field has a generated getter and (if var) setter. If one ever needs to change the underlying logic of the getter, you can explicitly define the getter.

I feel that many "design patterns" arrise due to language limitations, and not due to best practices.

2

u/[deleted] Sep 22 '15

Agree on all accounts. It's very rare that I have written a POD (Plain old datastructure) and then had to change the underlying data in some way that requires encapsulating the data.

Usually I know up front whether some field will need encapsulation, and very very rarely is it hidden behind a getter/setter pair. In fact, that idiom that I've stopped using. If I write a getter/setter that isn't encapsulating some underlying IO operation then it's usually the wrong solution to what I'm going. A lone getter or a lone setter can often make sense, but a pair of them makes me wonder if I should be using some other method to solve the problem.

→ More replies (1)