DEV Community

Discussion on: SOLID Programming (Part 2): Open/Closed Principle

Collapse
 
codbugs profile image
Coding Bugs

I don't think the code of this article shows what this principle is about. I think you are changing your code but not extending it.

Let me share what I would do with your sample. My starting point will be the first code and the adaptation to this principle will be to pass a method as an argument that do anything with the array of words.

def count_ocurrences(counter, localfile):
   content = return open(localfile, "r").read()
   return counter(content.split())

def count_and_word():
   '''this is the counter method to pass to the previous function'''

def count_or_word():
   '''another counter method'''
Enter fullscreen mode Exit fullscreen mode

This sample extends the functionality of the first function without modifying it. Same method, and different ways of counting words.