DEV Community

Discussion on: Avoid getters and setters whenever possible

Collapse
 
xowap profile image
Rémy 🤖

Well, I used to think that getters and setters are usually useless. Until one day I had to do some computations once a value was set and there I had to re-factor my whole code around the fact that the member was no longer directly accessible but had a getter and a setter.

So I'd say, putting getters and setters is more future proof whilst not being very expensive.

Yet I'd also say that this problem almost only exists in Java because other languages have ways around this (Python <3)

Collapse
 
rafiq74 profile image
rafiq74

I was about to mention the same thing. I had the same experience many times.

Collapse
 
jodydott profile image
Jody Dott

the computations should happen inside your object (in a OO world) you shouldn't be asking other objects for their internals (unless they are simple value objects).

Collapse
 
xowap profile image
Rémy 🤖

Yup but sometimes you start without needing the computation and then later realize that you need it for a new feature. In that case, it's hard to come back on your decision.

Thread Thread
 
jodydott profile image
Jody Dott

That's why you should program with an OO style and encapsulate. All requires computation.