+ 2

Best way to do modification without breaking OCP

Hi I have a very large code base and one file contains so many functions (not as a class members). For example, callback from 1 to 90. Now need to add one more that is 91 which is too similar to existing 90 I have two options: 1. Solution: Add callback_91 , copy code from 90 and modify this. Benifit: Dont want to change anything in working code of callback_90 Issue: Just repeated code and have very large code base now along with duplication. Another approach: 2. Solution: Create a common function and call this function from callback_90_New and callback_91_new Issue : Modified the existing code which is tested (callback_90 functionality) Benifit: Could avoid DRY(dont repeat yourself) Which is best and good to opt for ? https://code.sololearn.com/ccq1KiVV8Me2/?ref=app

4th Feb 2023, 9:33 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
5 ответов
+ 2
The common parts are usually extracted to a separate function that ONLY implements the shared parts. Then you can compose. To solve problems like this in object oriented languages, design patterns have been commonly used in the industry. Even if your code is not OOP, you can study these patterns and work out how pieces of your code could be combined. Since your own example is fake, I can only point to a few possibilities like Decorator, Adapter, Strategy, or Chain of Responsibility. https://refactoring.guru/design-patterns Repeating a single line of code, as long as it's a print statement, doesn't sound so terrible. But I imagine the actual code is not only about "cout". If there is a shared computation, refactor it into a separate function that returns the common result, or if it can be parameterized it's even better. As I said, composing impure functions (which print all over the place) is very difficult, so you should try keeping most of your code pure (no side effects).
5th Feb 2023, 7:39 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Another source I can recommend, about building clean and composable code, is a youtube video https://youtu.be/vK1DazRK_a0 If the link doesn't work the title is: Solving Problems the Clojure Way - Rafal Dittwald It explains refactoring through JavaScript but it's very easy to follow even if you don't know much JS.
5th Feb 2023, 7:42 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Clean code begins with using meaningful names. If you have callback_89 and callback_90 that suggests you already have a VERY serious design problem that calls for major refactoring. "Too similar" is meaningless. In your example you represent the difference with a conditional print statement. I wouldn't know if the real code is like this, but if possible, it would be better to use pure functions which have no side effects. In that case it is easier to compose them based on their shared functionality. https://www.freecodecamp.org/news/clean-coding-for-beginners/
5th Feb 2023, 6:33 AM
Tibor Santa
Tibor Santa - avatar
+ 1
I agree to your point about naming convention. But just not to disclose the actual methods , i wrote the names like this for query. I go through article shared by you, but still I am not clear. Got the point that if else or bool flag in function means violation of single responsibility. (My code has violation by using this in common function) But If i try to avoid that, I am repeating what is there in 90 into 91. Here it is just a small change but in actual scenario, it has so much meaningful. Copying from 90 to 91 will be repeatation of business logic. Right ?
5th Feb 2023, 7:10 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
5th Feb 2023, 9:16 AM
Ketan Lalcheta
Ketan Lalcheta - avatar