I am developing a small drawing application, with a little library of “complex” forms to be dragged and dropped.
I’m always in doubt when I’m growing a large hierarchy of objects extending from a bunch of others. Especially, I don’t always know what is best practice, to extend objects for pure code compression purposes, or to extend objects only when it semantically makes sense to do so.
Here I have a simple example. I have a square object with one property : edge. And then a rectangle and many other shapes. Should rectangle extend from square as it just adds a parameter ? Or should I do the reverse as square is a specialization of rectangle in maths ? Both ways seem to present issues.
Keep in mind that extending implies an is a relation. In your case, making rectangle extend square is like saying “A rectangle is a square” (which it’s not, necessarily, in terms of geometry). For this reason, it makes more sense to me to make square extend rectangle, since “A square is a rectangle” is more correct.
In general, it seems like having the “edge” property in square or rectangle is weird, if you plan to include “many other shapes”. I would suggest a Polygon class, which contains a set of edges, then a Rectangle or Quadrilateral class that extends Polygon, then perhaps a Square or Regular Quadrilateral class which extends that.