by dragly · 8y ago · 0 comments
It seems to be important to separate data from logic, and API from implementation in discussions like these. Sure, I can make a nice API for a visual Rectangle object with dynamic width and height, and add a check to my setWidth method to make sure the width is never negative and the changes trigger a repaint. But if I need a Rectangle struct to store data in the implementation of my library, it can be way simpler to just make the Rectangle have a public final/const width member, and set and check the width in the constructor.
Sometimes you need the smart Rectangle, and sometimes you need a dumb struct with width and height.
I think this is the essence of the "composition over inheritance" idea that seems to be the most violated piece of sound OO advice out there.
Most OO languages have type systems not fitting for such checks, though. Thus runtime checks, often a part of a setter.