I'd go as far as to say getters and setters are always a sign of bad design in OO code. Their only real purpose in that context is as a hack when business constraints don't allow for a more solid (you can also read that as SOLID) design.
Other than that, they're useful when one needs procedural segments in an otherwise OO codebase, mostly to implement some of the lower levels of the logic. Arrays just don't work well with "tell don't ask".
Learn something new every day.
- I am a senior software engineer working in industry, teaching and writing on software design, SOLID principles, DDD and TDD.
Location
Buenos Aires
Education
Computer Science Degree at Universidad de Buenos Aires
I'd go as far as to say getters and setters are always a sign of bad design in OO code. Their only real purpose in that context is as a hack when business constraints don't allow for a more solid (you can also read that as SOLID) design.
Other than that, they're useful when one needs procedural segments in an otherwise OO codebase, mostly to implement some of the lower levels of the logic. Arrays just don't work well with "tell don't ask".
I agree
Both setters and getters should be avoided most of the time.
People use them thinking they are protecting their implementation but is exactly the opposite. Next Sunday's code smell is related to that