by vbezhenar · 11y ago · 0 comments
Sometimes you want a computed property and you use non-trivial getter. Sometimes you want to do some work in setter. And then client's code will look terrible: point.x * point.x + point.getY() * point.getY(). There are other problems: binary incompatibility; non-trivial refactoring when I need to introduce a getter or setter.
Java really need lightweight property syntax which would solve all those problems. I don't understand why it isn't introduced yet.
[1]
public class MyImmutable {
public final String field1;
public final int field2;
public MyImmutable(String field1, int field2) {
// assign
}
// equals, hashcode, toString
}
[2] https://github.com/jqno/equalsverifierThat said, I agree that java _really_ needs something like this built in.
Then pre-compute it, make it immutable, and expose the variable. And if that ends up being too hard it's probably because you need a builder and/or to group your properties into smaller classes.