News for hackers. Stuff that matters.
Re:Data Classes for Java by chubot on Wednesday November 01, 2017 @09:10PM (#15607035)
FWIW, I've been writing software professionally for more than 15 years, and I've literally never come across this problem. It feels contrived to me.

However, there are many different types of programming, so I'll concede it the technique could be useful in domains I'm not familiar with. But if there any such situations, then I don't think they explain the wide popularity of getters and setters.

I rarely use getters and setters and it's never been a problem. I also don't use public fields unless the class is only public fields (and the language doesn't have some kind of record/tuple type), as another commenter mentioned.

Re:Data Classes for Java by hrktb on Wednesday November 01, 2017 @09:47PM (#15607184)
I think the popularity of getter and setters come from a mix of dozens of small problems to solve.

Protection from internal representation change is one. Expliciting what can be done with a property is another (i.e force write only or read only properties by only exposing a setter or a getter). Exposing “virtual” properties that are a combination of “real” properties (i.e. exposing a string representation of a price with a currency for instance, where both the price and currency are independant properties). Spying on getters and setters also helps for testing. And so many other potential uses.

Each use case is not a big deal on its own, but there is enough chance to hit at least one of the potential use case for at least some of the objects that autogenerating getter and setters for basically everything is seen as a good practice. Also no one gets fired for putting getter/setters, the reverse could be true.

I personally prefer contexts where I don’t have to care so much about all the above (this means less protective programming in general), but in a lot of enterprisey applications generating getter/setter everywhere is just a way of life.

Re:Data Classes for Java by coredog64 on Wednesday November 01, 2017 @09:49PM (#15607195)
How about this non-contrived example:

Certain data types (Array, Java.util.Date) expose private information if you just blindly return your instance variable to the caller. Setting can be dumb, but you may want to create a defensive copy to hand back on get.

C# syntactic sugar makes that apparent to the reader.