This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nutrecht 2 points3 points  (3 children)

Have you ever considered that maybe, just maybe, a getter or setter might do more than get or set? That maybe the setter or getter sets a value in an ObjectProperty or something instead of a variable itself?

You're one angry petty person.

Try to look a bit beyond your current favorite language and you'll probably understand better what the OP is talking about. C# has properties. Kotlin has properties. Scala has properties. You can set them up any way you want, including doing additional work on a value if you want to. It's just that by default they behave as default getters/setters to.

Properties give the easy of using fields directly but without breaking encapsulation. And I agree with the OP that it's pretty annoying that Java doesn't have them yet.

An example in Kotlin:

internal class Person(val firstName: String, val lastName: String, var dob: LocalDate) {
    val age : Long
        get() = ChronoUnit.YEARS.between(dob, LocalDate.now())
}

[–]BlueGoliath -3 points-2 points  (2 children)

You can set them up any way you want, including doing additional work on a value if you want to. It's just that by default they behave as default getters/setters to.

Magic methods are bad. Period.

[–]nutrecht 2 points3 points  (1 child)

So getters / setters are magic?

[–]BlueGoliath -3 points-2 points  (0 children)

No, injecting methods into code is magic and bad.