For most of my OO career (C++ for a long time, and then C#) I believed
in the OCP very strongly. It’s supposedly the cornerstone of OOP,
right? I came across this article today which brought it mind again:
But I remember my mind getting warped for a few hours when I was first
learning Ruby last year and came across Rails code that extended
existing classes so you could do this:
which I thought was just awesome. Ruby lets me change anything I want
about any class, it seemed, and it was a strange thing to me.
Of course, I’m not worried about breaking existing code when it’s just
my own code, but I’m still violating the open closed principle by
changing the Time class, right? But I think this ability is one of the
things that makes Ruby so great!
So what gives? Of course the idea of not changing behavior that clients
depend on still holds - but that’s something different, the concept of
So maybe I’ve misunderstood the OCP all along?
Or is just that we’ve come a long way since then?