News for hackers. Stuff that matters.
Re:Better Java – Resources for Writing Modern Java by vbezhenar on Saturday September 19, 2015 @07:28PM (#10245919)
Another problem with struct approach is inconsistency.

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.

Re:Better Java – Resources for Writing Modern Java by PascalW on Sunday September 20, 2015 @03:40AM (#10246854)
I'm surprised I haven't seen anybody mention the "public final fields" approach yet. I use this myself for immutable objects and I quite like it. Granted, you still need to implement equals and hashcode, but these can be generated easily and verified with tools like EqualsVerifier [2].

[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/equalsverifier
Re:Better Java – Resources for Writing Modern Java by jay-anderson on Sunday September 20, 2015 @01:10AM (#10246628)
Projects exist to to make a light weight property syntax. The most prominent might be @Getter and @Setter from lombok (https://projectlombok.org/), but there are others which have similar goals (e.g. http://immutables.github.io/, https://github.com/google/auto).

That said, I agree that java _really_ needs something like this built in.

Re:Better Java – Resources for Writing Modern Java by zmmmmm on Saturday September 19, 2015 @09:38PM (#10246249)
The lack of simple property syntax is probably the main reason I keep using Groovy now that Java 8 has arrived. Yes, state is evil. But there are still plenty of situations where you are just schlepping data around and generating thousands of lines of boilerplate to achieve simple data transfer is such a pointless and ugly hack.
Re:Better Java – Resources for Writing Modern Java by cam- on Sunday September 20, 2015 @03:45PM (#10248755)
For DTOs we don't user mutators. Just public member variables. It is the only way to keep business logic out of them. That being said, I wish Java has some object/struct syntactic sugar like Javascript Object Literals. They are so powerful.
Re:Better Java – Resources for Writing Modern Java by logn on Saturday September 19, 2015 @07:49PM (#10245977)
> Sometimes you want a computed property and you use non-trivial getter

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.

Re:Better Java – Resources for Writing Modern Java by barrkel on Saturday September 19, 2015 @08:05PM (#10246019)
Precomputing properties on a hundred million object instances is not cheap.
Re:Better Java – Resources for Writing Modern Java by jimueller on Saturday September 19, 2015 @08:39PM (#10246103)
Going from C# to Java, one of the biggest things I miss is the Property syntax.
Re:Better Java – Resources for Writing Modern Java by iconhacker on Saturday September 19, 2015 @11:43PM (#10246497)
C# property shares its own set of issues , especially when there are exceptions involved.