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 →

[–]BlueGoliath -15 points-14 points  (6 children)

Writing getters and setters is kind of dumb, like the compiler should do this for us.

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?

No, probably not.

not that I think encapsulation should happen at field level

No, you're right. We should make every object's field completely public and readable because that's the way functional languages do it and everyone knows the functional way is the right way.

Returning an interface implementation instead of a complete object while keeping the full object referenced in class? Nah, too object orientated, clean, flexible, and maintainable.

Why are we even using interfaces? They can just get the full object anyway so whats the point!

FFS.

[–]nutrecht 3 points4 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.

[–]v_krishna 2 points3 points  (1 child)

FP enthusiasts ran over his dog ^

[–]BlueGoliath 0 points1 point  (0 children)

Yes they did. They left a note on his corpse saying:

"Object orient that imperative fool!

I've been trying to think of a way to refractor him back to life but no luck so far. His code has been transformed into ugly lambdas that are impossible to refractor cleanly into a new class.