5 ms·
This is one where composition really does shine. A colored point is a point and a color. Make it so that it is explicit, and the code is much easier to reason
by taeric 6y ago
This is one where composition really does shine. A colored point is a point and a color. Make it so that it is explicit, and the code is much easier to reason about. Two colored points only equal if both components equal. And you have obvious ways of comparing the points and the colors.
- wvenable 6y agoIt doesn't much make a difference. If you want to write an equality method for ColoredPoint (containing a Color and a Point) then you're doing all the same work.
- taeric 6y agoYou dodge all of the difficulties of inheritance that they reference.
- legerdemain 6y agoOK, can you go into detail why equality between Point and ColoredPoint written using composition is simpler than equality between Point and ColoredPoint written using inheritance. Specifically, how does composition make equality transitive?
- taeric 6y agoA point can never equal a colored point. You can extract the point from a colored one to compare it to a point. That is, you can compare a point to the point of a colored point. But to compare a point to a colored point is essentially false, by definition.
- legerdemain 6y agoAh, so your point (ba-dum-ts) is that equality should just be type-invariant. That's a reasonable take, but it's a bit orthogonal to the larger conversation. In your invariant world, is the integer 2 equal to the floating-point number 2.0. Or is 2.0 actually a composition of an integer part and a fractional part, while 2 and 2.0 are not directly comparable?
- taeric 6y agoNumerical comparisons are already tough. Multi dimensional comparisons are easiest just dodged. Edit: forgot to say kudos on the joke. :)
- deleted 6y ago[deleted]
- wvenable 6y agoThis is true. The solves for Pitfall #4. But it solves for Pitfall #4 but just explicitly being different types. That works when you really want them to be different types. For this example, I agree -- ColoredPoint and Point should not be designed as the same type. In cases where inheritance makes more sense, the equals relationship is also easier to specify.
- taeric 6y agoI've seen frighteningly few places where the inheritance equality acted as was desired after more than a single collaborator. :(
- wvenable 6y agoI think the issue only exists for "value" types anyway. Reference equality works perfectly well for all types where I've used inheritance.
- taeric 6y agoReference equality's inability to survive a persistence round trip is the big killer. And sneaking in a round trip in a process that used to be fully in memory is where dragons lurk.
- wvenable 6y agoEquality is the least of your problems if you multiple instances of the same entity floating around.
- taeric 6y agoSometimes you are seeing if a constructed instance equals an existing one. I would presume that is rather common. Especially with points, a basic collision system is often basically an equality check. Right?
- cosmotic 6y agoComparing equality of color seems like a trap. If they aren't from the same color space, it's more like comparing a float, which will need some precision specified.
- taeric 6y agoI dodged that dubious point. :) And, the same goes for points, all told. Does the 2d point (x,y) equal the 3d point (x,y,0)? Basically, equality works best when you greatly limit the scope and don't try to get fancy.