News for hackers. Stuff that matters.
Re:Are software engineering “best practices” just developer ... by Jtsummers on Thursday September 30, 2021 @12:48PM (#28708376)
Getters and Setters also permit you to specify that something can only be retrieved or assigned, but not the other. This is non-trivial in most programming languages so having this as a common pattern is useful. Though I like C#'s properties versus seeing a bunch of getFoo and setFoo methods running around, if Java had provided the same or a similar capability there would probably be no controversy. It's the noisiness of the Java solution that seems to irk people more than the concept.
Re:Are software engineering “best practices” just developer ... by shagie on Thursday September 30, 2021 @01:04PM (#28708569)
Java now has records which handles the creation of getters (and since its an immutable, it doesn't have any setters).

https://blogs.oracle.com/javamagazine/post/records-come-to-j... and https://dzone.com/articles/what-are-java-records

I'm really looking forward to using the local records defined mid stream for bundling the information to the next part.

Re:Are software engineering “best practices” just developer ... by avgcorrection on Thursday September 30, 2021 @03:08PM (#28710038)
> Getters and Setters also permit you to specify that something can only be retrieved or assigned, but not the other.

Only retrieved? `final` and set the fields through the constructor.

Sure someone could implement setters-only if they wanted that. Except I’ve never seen it and get/set are always used instead. Just one layer of indirection for no benefit.

Re:Are software engineering “best practices” just developer ... by Jtsummers on Thursday September 30, 2021 @03:14PM (#28710114)
That's not equivalent. You may want a field to be updated by other actions. That is, its value is derived from the other properties of the system. You could recompute those on each request, but that's potentially costly. You could also recreate the entire object with each small field change. That's also very costly.

Consider a collection class which has a size field. `final` would be the wrong thing to use. If you expose the size field but make it final, well, it's immutable and your entire collection is either lying or also immutable. If you expose the size field but it's not final, then anything can alter it. Consequently, a getter (the concept of one at least, see C# again for a cleaner approach than what Java did historically) is useful and then when another element of the interface is used (adding or removing elements) the size field can be updated appropriately. The next time it's retrieved, it's correct and you don't need to recompute it.

This kind of thing can be a consequence of many other systems, a collection is just sufficiently universally understood that it's a good example of the concept.

Re:Are software engineering “best practices” just developer ... by avgcorrection on Friday October 01, 2021 @01:03AM (#28714754)
A collection is a red herring. A collection uses a “getter” for the size since the size is not something you get/set like in a value object.

But then again no one calls it a “getter” (not even Java calls it `getSize()`) since the get/set discussion is about updatable fields, not the inner guts of objects.

Has anyone argued against encapsulation? No one has.

Re:Are software engineering “best practices” just developer ... by ludston on Friday October 01, 2021 @03:34AM (#28715688)
C# properties are good until you need them to be delegates, and then they are annoying.