Mental Floss: What's the Problem With My Circle

I got into an interesting discussion about SOLID principles with a fellow developer today, and it got me to thinking about a common problem I’ve seen come up and I wanted to try and touch on it.

So first things first, let’s go over some math and set up the problem space.

You have an Ellipse. You have a Circle. A Circle is a special kind of Ellipse. Specifically, an Ellipse has a major axis and a minor axis, and a Circle is special because it’s major and minor axis are equivalent in their length.

So when a developer initially goes to model these objects, you typically would see something like this:

I see this implementation a lot. When asked how this supports SOLID principles, I get the following a lot of the time:

It supports OCP because I can override any of the properties or the methods. Ok good.

It supports SRP because the class is only responsible for knowing things about Ellipse. Ok good.

And there are some other ones, but then we veer into trying to extend this model with the special Circle, and that’s where things really start to break down. I’ve seen a lot of variations about how this goes down, but I’m going to summarize all of them with this definition:

What happens here is a new property is introduced so that the users know that Circles are special because they have a Radius. Then when we calculate the Area, we override the function that calculates the area and we use the radius instead.

So someone would use it like this:

So what’s the problem with this? Think about it an tomorrow I’ll post my proposed solution.