Live data from Hacker News

Data Classes for Java

cr.openjdk.java.net

81–90 of 216 posts

Re: Data Classes for Java

#81
post #68

Earlier quoted context omitted.

Why have a mutable class to represent temperature - a simple value class with two fields? Why not just construct a new Temperature? I mean if performance was an issue, you'd just use plain doubles instead, right? I'm open to the idea that setters do have a use case, but I'm not seeing one here.

How about a class that stores temperature and the current state of the satellite? It's easy to come up with circumstances where requirements change.

Then I would implement it as a "real" class, not a data one.

    class Satellite {
        Coordinates coordinates;
        double celcius;

        /*** constructors, etc ***/

        public double Celcius() { ... }
        public double Kelvins() { ... }    
        public void Move(...) { /* modifies state */ }
    }
Note how none of the methods make any mention of low level operations like setting/getting internal fields.

You could make the argument that I do have a getter there, Celcius. But at the risk of pedantry I'd argue it isn't so. The name doesn't imply that it's retrieving the value of a field - ie the internal representation could easily change to Kelvin or Fahrenheit - where as "getCelcius" definitely tells me there's a field there called Celcius.

Hmm, after writing that I wonder what the definition of a setter actually is. I have no problem with a method that just returns a field, as long as it doesn't go out of its way to tell me that's exactly what it's doing. Maybe the distinction I am making is too subtle.

Re: Data Classes for Java

#82
post #53
post #18

Why underscores in "__data"? Is this just a temporary thing?

Probably to avoid making 'data' a reserved word and breaking a ton of code that used 'data' as a variable name. Prior to Java 5, 'enum' was a very popular variable name too.

I'm having a difficult time trying to think of a case where "data" is the best variable name for anything.

Re: Data Classes for Java

#83
post #3

I, for one, am enjoying the ever so slow scalaization of java.

But it also can be annoying as a Scala programmer because you are going to have to be aware of both the Java and the Scala versions of each thing and how they differ. For example, under this proposal, you will need "new" when creating an instance of a Java data class, but you are probably in the habit of omitting it when creating an instance of a case class.

this is why God invented the red squiggly underline in IDEs

Re: Data Classes for Java

#85

Earlier quoted context omitted.

I really, really dislike Swift's approach. get/set/willSet/didSet -- so much complication to preserve the illusion that you are operating on a field when you are actually doing no such thing. Why is this desirable? It reminds me of a class of C++ footguns where some innocuous code is actually invoking member functions due to operator overloading. I think that Java got this one right. (I do not at all like the cargo c…

Today's fields are tomorrow's computed properties. Fields are rigid and cannot be changed without recompiling dependencies. Notice how few fields there are in Java's standard library. Why have fields at all?

How many times have you run into this in your career (the need to convert "today's fields to tomorrow's computer properties")?

Re: Data Classes for Java

#86
post #37

AutoValue with Builders is nice: Animal dog = Animal.builder() .setName("dog") .setNumberOfLegs(4) .build(); https://github.com/google/auto/blob/master/value/userguide/b...

Not that nice compared to kotlin: data class Animal(dog: String, numberOfLegs: Int) val dog = Animal( name = "dog", numberOfLegs = 4 ) The amount of boilerplate for AutoValue builders is so ridiculous that most people use IDE plugins to generate it.

In clojure I'd write this, if I actually needed a class for type dispatch or interface implementation for some reason.

    (s/defrecord Animal
      [name           :- String
       number-of-legs :- Integer])
    
    (def dog (map->Animal 
               {:name "dog"
                :number-of-legs 4}))
The nice thing is that I could also skip the defrecord if I just want enforcement of the shape of the data by making a similar defschema and then I'd just use plain maps.

Re: Data Classes for Java

#87
post #9

Earlier quoted context omitted.

You'll get you run out of town with that level of heresy in most java/c# shops. Getters and Setters are great when you want finer control over what can access certain fields, in a List class for instance, I wouldn't want the length to be externally writable. But somewhere along the way it went from a useful feature in some circumstances to a mandatory feature in all circumstances. In the c# world "public int Id;" wil…

I don't understand your complaint. The problem with getters and setters was the substantial tedious boilerplate required. C# completely does away with that to where there is no meaningful difference between the two in terms of typing required, while at the same time providing the benefit of encapsulation. So what's the problem? Do you really object to 14 more chars to type? That's absurd.

> The problem with getters and setters was the substantial tedious boilerplate required

The problem isn't the tedious boilerplate, it's that it isn't necessary most of the time, 99%+ of getters and setters could be replaced with public fields. c# solved a problem that only existed in the first place because of cargo cult practices.

> while at the same time providing the benefit of encapsulation

It potentially provides encapsulation and I don't really object to them there, but if we just used them where they were really needed then we having a setter/getter function is would have been just fine as well and the language would be simpler.

> Do you really object to 14 more chars to type? That's absurd.

I see you didn't use c# before version 3. Originally they required almost as much boiler plate as java, I've still got a mountain of code around here from that time and all they add to the codebase is noise.

Re: Data Classes for Java

#89
post #37

AutoValue with Builders is nice: Animal dog = Animal.builder() .setName("dog") .setNumberOfLegs(4) .build(); https://github.com/google/auto/blob/master/value/userguide/b...

Not that nice compared to kotlin: data class Animal(dog: String, numberOfLegs: Int) val dog = Animal( name = "dog", numberOfLegs = 4 ) The amount of boilerplate for AutoValue builders is so ridiculous that most people use IDE plugins to generate it.

  data class Animal(dog: String, numberOfLegs: Int)  
and

    Animal dog = Animal.builder()
    .setName("dog")
    .setNumberOfLegs(4)
    .build();
are very different. In this case the builder pattern is more powerful as it lets you pass around builders that haven't been "built" yet. Kind of like currying.

Re: Data Classes for Java

#90
post #57
post #10

Earlier quoted context omitted.

> obfuscating what happens when you actually access them This is the point, AFAICT. In languages with both primitive "read/write field" operations that can't be interceded upon, but also with interfaces/protocols, "obfuscating" (encapsulating) a field access in getter/setter methods is how you allow for alternative implementations of the interface/protocol. Specifying your object's interface in terms of getters/sette…

Design patterns are bug reports against your programming language. -- Peter Norvig Java sure has a lot of patterns.

Appeal to authority. Show me a language with little to no patterns.
Post reply on HN