Hacker News new | ask | show | jobs
by Fatalist_ma 2224 days ago
I don't think this is a good or even valid example. Where do we see that the method that depends on the Rectangle class no longer works correctly for instances of Square? Why does it depend on the exact value returned by the object?

The example from the Stackoverflow link makes more sense: " ssert.AreEqual(20, rect.Height); ... because changing a rectangle's width does not affect its height. " Well, we may choose to not follow that assumption in our project. Let's say we're making a graphical editor, our rectangle could have various restrictions - like fixed proportions, max/min area, etc; so I can imagine how we may not have a guarantee that changing 1 side does not change another side. If it's just for general-purpose geometric calculations - then sure, but then it better be an immutable object.

I mean, you should not use inheritance for things like these but I don't think this always violates LSP.