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.)
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?
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;
}They're quite a smell that you have no encapsulation, plus mutable state.
I find myself either
- Writing object oriented code with higher level operations that operate on data internally without exposing it via getters and setters.
or
- Value types that take values in constructor and provide naming and additional behaviour on that data (proper Value Types support will really help with this.)
or
- Data pipelines dealing with n-tuples. For which you might be tempted to reach for getters/setters but I'd tend to either use off the shelf tuple types. i.e. tuple(A,B,C) or classes with public fields, or interfaces with static factory to create instances. I'd love better support for tuples with named attributes (Like new c# has)
Loads of getters/setters is something that the frameworks of the last decade encouraged people to do but aren't really a thing any more.
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)
Compare with Kotlin:
class MyFoo(val foo: Foo)
Done.Now you're left with something that could be fit on three lines (the "final" for the parameter is also useless and can be removed):
public Foo getFoo() { return foo; }
public void setFoo(Foo foo) { this.foo = foo; }
Even if you don't write the actual source like that, a good editor like IntelliJ can automatically display them like that (code folding).The C# version is about the same length:
public Foo Foo {
get { return foo; }
set { foo = value; }
}
Also, you don't have to write the get/set methods yourself. You hit a few keys and your editor generates them.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.
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.
So you don't like either of the following:
myObject.name = "Ben";
myObject.setName("Ben");
What alternative would you suggest then?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.
class Circle
{
public Circle(int radius) { Radius = radius; }
public int Radius { get; }
public int Diameter => Radius * 2;
} public int Radius;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.