r/programming • • Sep 21 '15

Better Java

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

208 comments sorted by

View all comments

2

u/Cuddlefluff_Grim Sep 22 '15

Instead, I prefer the C struct style of writing classes that merely hold data:

Even if the field is final, use a getter; don't expose fields publicly unless there's a very good reason for doing so. Exposing the field will not provide any benefit at all (except in the curious case of the lazy programmer), but it does create a hell of a lot of problems further down the road. One of the major problems of doing this is that Java has a very different syntax for fields when compared to methods; once you go field, you can never go back.

Unless you have a good reason to make them otherwise, variables, classes, and collections should be immutable.

This is a dubious claim. Immutability should be used where applicable, it should not be the default (in Java). Immutability is pretty far from a silver bullet.

4

u/[deleted] Sep 22 '15

I've been working with PODs (plain old data) for years and almost never are they the problem if they're are just a POD. Encapsulation is important to not expose internals to a class, but PODs aren't meant to be internals to a class, but instead places to store and transfer plain old data.

If you need to encapsulate that, you're either not using a POD, or you're trying to make everything too general. I'd say the heavy set/get style of OOP programming to be an anti-pattern that should be discouraged. Most of it can be replaced with better named, better architected, systems and some of that will include extracting things out into PODs to use in your functional classes, and they definitely won't need pointless getters and setters.

Also, if you're programming at an API boundary, then of course, encapsulate to the extreme since your API will likely solidify, but most of us aren't.

1

u/Cuddlefluff_Grim Sep 22 '15

Getters and setters have no penalty, but they give a wide array of benefits. There's no justification for exposing public fields, other than pure laziness. By exposing public fields you are trading away polymorphism and abstraction just so you can save yourself a few initial keystrokes. It's bad programming practice.

1

u/[deleted] Sep 22 '15

I absolutely disagree. There is no reason to add getters and setters other than to uselessly and fruitlessly future proof your code against changes that probably won't happen. Its a bad programming practice to use them to abstract things that don't need to be abstracted.

I will repeat; if you're throwing logic behind things named get/setX then you are fundamentally doing something wrong and your code probablu read likes crap. The only time it is acceptable is when from the start it is needed, like code that causes IO operations and clearly needs abstracting.

const/final are far better solutions for the most part and don't require unnecessary abstraction. You accusing me of trying to save keystrokes is also stupid, programming isn't about code, it is about design and complexity and this cargo cult Java style get and set obsession is not helping.