News for hackers. Stuff that matters.
Re:A Square Is Not A Rectangle by barrkel on Monday September 14, 2009 @05:45PM (#822681)
This just comes down to the difference between identity that changes with mutation, and identity which is constant with mutation.

All squares are rectangles. Some rectangles are squares. If the object used to represent squares and rectangles is mutable in width and height, then its squareness cannot be immutably associated with its identity, and thus cannot be encoded as a type, since objects in static languages have immutably associated types.

There's some deep point about naming objects after their attributes, without paying attention to whether the attributes may change. Experienced programmers know this well, but laypeople don't need to make the distinction often as their language usage is usually only temporary, not needing to account for changes over time. It's the statement that must always be true, when all the right qualifiers and conditions have been included, that's hard to make.

Re:A Square Is Not A Rectangle by joe_the_user on Monday September 14, 2009 @07:24PM (#822862)
No, there is no easy answer to this problem in a way that jives with all doctrines and also works in practice.

OO is ambiguous as to whether a "subclass" of an object is specialization of the parent object or a generalization. In practice, most subclasses are a generalizations and specializations introduce all sorts of problems, especially enforcing constraints.

This naturally comes down to OO trying to shoe-horn (at least) four related-but-not-necessarily-identifical activities together: code/data encapsulation, program design, code-reuse, and modeling (the translation of real-world objects-and-categories to code).

The net result is that OO languages, tools and methods can be useful for writing large program and can give you effects that are better than the alternatives but that the pronouncement of OO gurus wind-up sounding idiotic and following the book to the letter can create situations that negate the particular benefits of OO.

Re:A Square Is Not A Rectangle by barrkel on Monday September 14, 2009 @09:48PM (#823060)
By Liskov substitution principle, or with the set view of types, subclasses are always specializations / subsets, unless you're introducing some kind of home-baked type system built on top of the existing type system.

I would classify the parts of OO differently. I think your second and fourth activities are the same, but one models abstract concepts, and the other models concepts of concrete things. I wouldn't bring in code reuse at this level; every practical programming paradigm tries to include code reuse. It's the mechanism that's a distinguishing feature, and the mechanism OO uses is a combination of subtype polymorphism and dynamic dispatch. It's a baked-in version of the function table / union|variant record idiom from procedural programming, and it has a transposed expression in sum types and pattern matching in functional programming: OO limits the addition of new (virtual) methods (on the base) but accepts the creation of new types (even at runtime), while FP limits the creation of new types (in most implementations I've seen) but accepts the addition of new methods (which costs some in discoverability in modern IDEs, and has namespace implications).

Re:A Square Is Not A Rectangle by joe_the_user on Tuesday September 15, 2009 @01:38AM (#823345)
Hmm...

By Liskov substitution principle, or with the set view of types, subclasses are always specializations / subsets, unless you're introducing some kind of home-baked type system built on top of the existing type system.

So, does that mean that subclasses shouldn't have extra member variable in the way that a square doesn't have any extra member variables in inheriting?? I know that there are some systems which try to do something like this - Darwin and Date's Third Manifesto, for example, though they don't try to say they're doing OO. And, of course, this is far away from what most people do in defining objects.

The various tools and approaches are useful and your particular take on it might make it consistent if it was generally accepted. But at this point, the many confusions about what OO are set in people's minds. What's being defined now is Agile - which is naturally another semi-original, semi-useful, semi-contradictory toolset.

But hey, I guess my conclusion is it's all good if you don't take it too seriously and do stupid things just to satisfy some absurd constraint - like in the hay-day of OO when people let go of common sense and built these huge, fragile multiple inheritance trees that wound up not having the slight understanding of - or used ten separate, nested classes to define the complex numbers...

Re:A Square Is Not A Rectangle by barrkel on Tuesday September 15, 2009 @02:14AM (#823380)
> does that mean that subclasses shouldn't have extra member variable

Not sure what you meant here. Extra members, whether they be properties, fields, methods, types, whatever, should be fine in subclasses. If it was removing members from the superclass, that would be problematic from a subtyping / polymorphic perspective, but adding members makes it more specific.

Re:A Square Is Not A Rectangle by davidmathers on Monday September 14, 2009 @07:29PM (#822876)
This just comes down to the difference between identity that changes with mutation, and identity which is constant with mutation.

What you say is true RE the OP, but the OP is not very good. Mutability is a red herring. Uncle Bob points this out in his response.

Assume immutability. Now the names 'square' and 'rectangle' actually make sense and the real issue becomes clear: the correct number and names of variables needed for representation. And the definition of the 'area' method.

Circle-ellipse is a better illustration because a circle has a 'radius' property that an ellipse doesn't have.

Re:A Square Is Not A Rectangle by barrkel on Monday September 14, 2009 @09:36PM (#823044)
Ellipses have major and minor radii; in circles, both are the same length. Attributes in these kinds of objects are a bit of a red herring, though, as one set of attributes can be calculated from another, and vice versa. A circle can be described as a parametric function of angle, x = r * cos(t), y = r * sin(t); or the more familiar r^2 = x^2 + y^2. Which attributes you choose as the canonical, and which you choose to calculate, is just an implementation detail.

FWIW, I've felt for some time that the benefits of object orientation have been over-emphasized, though the general pattern of industry and language development has been moving in a better direction of late (say past 5 years).

I also have no well of respect for Uncle Bob - that's not to say he isn't a very intelligent, wise guy with lots of insightful things to say, it's just that I haven't read much of his stuff, and had never heard of him until a few months ago. But when I read him try to make an analogy between types in a program representing things in the world, and two lawyers advocating for a divorcing couple, and saying that since it's not the lawyers that are divorcing, you shouldn't model your types in an isomorphic correspondence to the things in the world you're modelling, well then, I think he's overreaching. If all your world is immutable rectangles, and you need to handle squares differently, and you're solidly orthodox in your subscription to OO, then I think modelling the relationship with inheritance is perfectly fine. In static languages, the fact that inheritance creates a subtyping relationship and introduces the possibility of polymorphism and dynamic dispatch is usually more interesting than the inherited scope of member lookup.

Re:A Square Is Not A Rectangle by davidmathers on Monday September 14, 2009 @10:08PM (#823086)
Ellipses have major and minor radii; in circles, both are the same length.

This is exactly the issue. According to specialization by constraint when an ellipse has equal radii then it is both an ellipse and a circle, therefore a circle isa ellipse, therefore a circle is a subtype of ellipse.

Hence a square isa rectangle. The specialization by constraint argument is that any type system that requires a rectangle to be a subtype of square or both to be subtypes of polygon is inherently broken.

LSP says that if a circle is a subtype of ellipse then it can be used in place of an ellipse. But it can't, so a circle can't be a subtype of ellipse. LSP and specialization by constraint are not compatible.

Re:A Square Is Not A Rectangle by barrkel on Tuesday September 15, 2009 @02:04AM (#823372)
LSP says that if a circle is a subtype of ellipse then it can be used in place of an ellipse. But it can't

You haven't shown that - you've merely asserted it. If you live in a world of immutable ellipses, and you need different behaviour from circles and non-circle ellipses, you can make circles subtypes of ellipses without any problems, and still satisfy LSP - because we're not specializing by constraint. The most important point: circleness is attribute of an ellipse, and if the ellipse is immutable - its radii can't be changed - then that circleness can be contained in the type without problem.

Re:A Square Is Not A Rectangle by davidmathers on Tuesday September 15, 2009 @04:26AM (#823496)
You haven't shown that

I don't understand. An ellipse contains more information than a circle. You can't ask a circle for the length of its minor radius, so how can it substitute for an ellipse? I'm not sure we're speaking the same language.

Re:A Square Is Not A Rectangle by barrkel on Tuesday September 15, 2009 @06:09AM (#823566)
I believe you're assuming more things about the programmatic interface of a circle type than is necessarily true. An ellipse with equal major and minor radii is a circle. That much is mathematical fact.

Equivalently, a circle is a special kind of ellipse. As an ellipse, you can certainly ask it for major and minor radii. But "more information" is an implementation detail. If Circle were a subclass of AbstractEllipse, which itself also had a concrete Ellipse subclass, Circle might implement both major and minor radii "properties" in terms of a single field. But again, this is just an implementation detail.

All this is only practically useful if there's a reason for considering circles as ellipses, or for specializing behaviour for circles. For example, the circumference calculation for circles is much simpler than for ellipses, so we might want to specialize it. Since our values are immutable, there's no problem in capturing this optimization in the type and overriding the appropriate method.

(I'm assuming immutable values throughout, to make it clear.)

Re:A Square Is Not A Rectangle by davidmathers on Tuesday September 15, 2009 @05:00PM (#824748)
You seem to be saying that circles are not subtypes of ellipses. That circleness is just a property of ellipses. If we accept that then the whole issue is moot, and substitutability is true by tautology.

A circle is a triple: (x,y,radius). An ellipse is a 5-tuple: (x,y,major,minor,angle).

Maybe we should be talking about angle instead? It can't be derived from a circle. What happens when you feed a circle to selectEllipsesByAngle()?

Re:A Square Is Not A Rectangle by barrkel on Tuesday September 15, 2009 @09:29PM (#825142)
If isEllipseAtAngle(someAngle): boolean were a method, a Circle type could return true for every angle.