8 ms·
Am I missing properties? Dear god it's 2016, with Java8, and we still don't have real, language-defined properties? I mean you can use Lombok (and if you're not
by djsumdog 10y ago
Am I missing properties? Dear god it's 2016, with Java8, and we still don't have real, language-defined properties? I mean you can use Lombok (and if you're not, you should be!), but that's something that really need to be built into the language. C# has them, Python has them, Ruby has them .. even other JVM languages like Scala have them!
I'm really glad I get to do Scala development full time. Even with all the recent improvements to Java 8, I feel the future of Java isn't the language, but the JVM and all the other languages that run on top of it (Scala, Groovy, Clojure, JRuby, etc.)
- pjmlp 10y ago> Scala, Groovy, Clojure, JRuby, etc. I am yet to work in a Java project where I am allowed to use any of them. Regarding the properties I really don't get what is the big deal. No one used to complain about C++ properties, that besides having to write accessors and mutator methods, one needs to declare them on the header files as well. Or the first version of C# properties isn't much shorter than how it is done in Java, which is still required when extra logic needs to be implemented. It is been a few years since I have done any Python, but don't do they require two separate functions that are then mapped to a property declaration?
- josho 10y ago> Regarding the properties I really don't get what is the big deal. I used to feel the same as you, then I dabbled with Rails. I think the deal is that in Java there is a lot of noise in the code. So, developing feels like you need to do a lot of yak shaving before you get to actually work on what you intend to build. As a result, properties on their own isn't a big deal, but it's one more thing to slow you down and make things a little less enjoyable.
- dmux 10y agoAre people manually writing getters and setters? Every Java IDE I've used has the ability to generate them for you.
- djsumdog 10y agoCode generation is a design smell. You shouldn't need it, and it makes the code terrible and less readable. Use Lombok, not the IDE.
- agentgt 10y agoWhen I see some one complaining code generation being design smell I gag a little at this hackneyed Fowler expression. I'm curios what do you think lambok does?
- nine_k 10y agoIt does code generation. What id does not is _source_ code generation. That is, you never see the code Lombok generates, never have to maintain it, and never have a chance to break it by editing. You can see it as a macro substitution. OTOH IDE-generated code is "normal" source code, adding boilerplate to your "real" code, not tracking changes in it automatically, and perfectly breakable.
- st3v3r 10y ago"Code generation is a design smell." What do you think Lombok is doing?
- kentosi 10y agoI think that the point is that you shouldn't have to rely on an IDE in the first place.
- jjoonathan 10y ago> isn't much shorter than how it is done in java The savings in screen space and visual cortex neurons doesn't come from the one or two properties that need logic, it comes from the dozen avoided /** * Gets the foo * @return the foo */ public Foo getFoo() { return foo; } /** * Sets the foo * @param foo */ public void setFoo(final Foo foo) { this.foo = foo; }
- fauigerzigerk 10y agoAdding 8 comment lines doesn't help your argument. More importantly though, you need to consider the mental load of constantly thinking about and remembering whether something is a property or a method. I used to be an advocate property syntax, but after seeing them used in C# and now in Swift I have completely changed my mind. It's an inconsistent mess that Java has avoided at the small price of some more lines of code (that are mostly auto generated and need no documentation)
- int_19h 10y agoCan you clarify what's inconsistent about properties in C# (seeing how this is the most obvious counterpart), and how it doesn't apply to Java's properties-by-convention?
- fauigerzigerk 10y agoIn Java you can be reasonably sure that whenever you need something from an object you call a method of its class. That's one thing you rarely have to think about in practice. Yes there are exceptions (sometimes painfully inconsistent ones) where fields are accessed directly, but at least that gives you some performance guarantees. It's generally not a big concern. In C# you do have to think about it all the time. Most types have both properties and methods and in many cases it's not obvious whether the author of that type had the same idea about whether or not something should be a property or a method as you do. Where you only have to remember a name in Java, you have to remember both a name and its syntactic category in C#. One more thing to keep in mind equals greater mental load. In fact, the guidelines for choosing between one and the other include considerations that should be of no concern to the user of a public interface (https://msdn.microsoft.com/en-us/library/ms229054(v=vs.100).aspx https://msdn.microsoft.com/en-us/library/ms229054(v=vs.100)....). If any of these implementation details change, the logical conclusion would be to change the public interface from property syntax to method syntax or vice versa. That's what I call inconsistent. If you're saying that the choice between getX() and x() in Java is also somewhat inconsistent then you are absolutely right. It's just not a problem on the same scale as in C#.
- carterehsmith 10y agoProperties are not a huge deal, I agree, but they sure are annoyance in Java (compared to e.g. C#). That is, instead of one line of the code declaring the property, we end up with 10 lines doing the same thing, because we also need to declare getter and setter. So, a realistic "POJO" that has 8 properties, will have 8 lines of code in C#, but 80 lines of code in Java. Sure, your IDE will generate the getter and setter, but when you are reading the code, you have to check whether getter and setter are trivial, or perhaps they do something else, too. It is quite common to get burned by a setter that someone made so that, after setting the value, it also does other shit like, hit Google Analytics, which is why everything is so slow. The above problem is super easy to spot in 8 lines of code, but much harder to spot in 80 lines of monotonic, repeatable code, especially so when they are decorated with another 100 lines of code that is IDE-generated comments like 'Sets the foo'/'Gets the foo'. My 5c.
- aardvark179 10y agoProperties mean a lot of things to different people, and C#'s can do stuff that you don't expect so I would guess won't make it into Java as they tend to prioritise reading code over writing it. Having said that if you look at https://www.oracle.com/javaone/on-demand/index.html?bcid=5131218264001 https://www.oracle.com/javaone/on-demand/index.html?bcid=513... about 30 minutes in you might see Brian talking about something that may be close enough to what most people want from properties to satisfy them.
- int_19h 10y ago> Properties mean a lot of things to different people Thing is, this is true whether your language offers syntactic sugar for them, or not. Properties already exist in Java, they just exist as a matter of convention and custom - but the expectations on how they behave are just as strong, and a misbehaving getter, say, is just as surprising and damaging.
- lacampbell 10y agoI really don't understand why people love properties so much. Getters and setters are elements of bad style to me - like global variables or gotos. A language like Java (or indeed C#) - specifically designed for huge teams and enterprise programs - shouldn't provide syntax sugar for writing getters and setters.
- kentosi 10y ago"I really don't understand why people love properties", followed by "Getters and setters are elements of bad style". So you don't like either of the following: myObject.name = "Ben"; myObject.setName("Ben"); What alternative would you suggest then?
- lacampbell 10y agoProbably a rethink of the design. Why are you creating an object, then mutating its name from client code? The whole point of an object is that client code shouldn't be concerned with such details as maintaining the state of each internal field. Try and think of an object as a single thing you send high level messages to you, instead of a struct, or a bag, or a hashtable.
- chrisseaton 10y agoWhat would a high level message for changing the name of some object look like, if not setName(newName)?
- marc_omorain 10y agoCalls to mutate state, such as changing the name field, should be rare. I don't see the value in adding syntactic sugar to make it easier to mutate state.
- carterehsmith 10y ago"Calls to mutate state, such as changing the name field, should be rare." Says who? For example, there is a business requirement that we know the on-hand quantity of some item in the warehouse. It's a busy warehouse, and we keep picking that item for delivery, and replenishing it from our suppliers. So, the quantity of the item keeps mutating. How do you propose to deal with the above? Tell the warehouse people that they should only do their thing "rarely"? Seriously.