Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In OOP there is the Open/Closed principle that states that

"software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification".

Taking in consideration that in Ruby it is to possible to reopen a class, don't you think that this breaks the Open/Closed principle?

When do you think we should favor reopening classes instead of just extending them?

What do you think that are pitfalls in reopening classes?

share|improve this question

migrated from stackoverflow.com May 10 '11 at 17:03

This question came from our site for professional and enthusiast programmers.

    
@Tomasz, I disagree. This question deals with a particular issue or Ruby with concrete consequences. –  John F. Miller May 10 '11 at 16:20
add comment

2 Answers

up vote 5 down vote accepted

Ruby's open classes provide a way to programatically build up complex classes. For complicated libraries this makes configuration easier, and code smaller and more maintainable. If you ever meet a Lisp programmer, they will talk you ear off about how wonderful it is to use code to write code.

This power can be used for both good and evil. The practice of Monkey Patching functionality into a class is expedient, but is also a form of technical debt that will make it harder to stay current and maintain code.

A good rule of thumb is that a class should be reopend only by the person (group) that originally wrote it. If others are feeling then need to reopen one of your classes, that is probably a sign that it needs to be refactored so it is more configurable, that is open for extension. This is the D of SOLID.

share|improve this answer
add comment

I found the podcast Monkeypatching and the Open-Closed Principle is discussing this very topic in detail. check out.

Cheers

share|improve this answer
1  
Watched this for four minutes, then gave up. Please tell us when these guys finally get to the point. –  larsmans May 10 '11 at 17:16
    
@larsmans 07:10 minutes into the video. –  conor hogan Apr 30 '12 at 21:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.