Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring a class in C++

I have a class A which implements many functions. Class A is very stable.

Now I have a new feature requirement, some of whose functionality matches that implemented by A. I cannot directly inherit my new class from class A, as that would bring lot of redundancy into my new class.

So, should i duplicate the common code in both the classes?

Or, should i create a new base class and move the common code to base class, and derive class A and the new class from it? But this will lead to changes in my existing class.

So, which would be a better approach?

like image 935
chappar Avatar asked Jan 28 '09 18:01

chappar


People also ask

What does it mean to refactor a class?

Refactoring consists of improving the internal structure of an existing program's source code, while preserving its external behavior.

What is code refactoring in C?

Code refactoring is defined as the process of restructuring computer code without changing or adding to its external behavior and functionality. There are many ways to go about refactoring, but it most often comprises applying a series of standardized, basic actions, sometimes known as micro-refactorings.


2 Answers

Unless there is a very good reason not to modify class A, refactor and make a common base (or even better, a common class that both can use, but not necessarily derive from).

You can always use private inheritance to gain access to the shared functionality without modifying class As external interface - this change would require a rebuild, but nothing more. Leave all the functions on class A, and just have them forward to the shared implementation class.

One reason you might not want to refactor, but rather copy the code is if it's likely that the new class' functionality will change, but without the same change being needed in the old class. One of the reasons for not duplicating code is so that a fix or a change needs only to be made in one place. If changes are going to happen that will break the original class, then maybe you want to copy the code instead. Although in most cases, this will only happen if the two classes aren't as similar as you thought, and you'd be fighting to try and abstract a common set of functionality.

like image 50
Eclipse Avatar answered Sep 30 '22 18:09

Eclipse


(1) Do not duplicate the common code in both classes unless you or your employer are prepared to maintain both copies indefinitely.

(2) Refactoring by definition changes classes you already have. The refactor you propose is called "Extract SuperClass" followed by "Pull Up Method" for each method that is common to the derived classes. This is a fine approach.

Edit: I remembered the real gem behind Refactoring: Within reason it is perfectly fluid and reversible. There is never a "Right" there is only a "Right for now". If you decide later that using inheritance for these classes isn't a good object model then you can refactor again to use composition (as superbly suggested by Josh).

like image 34
Jason Punyon Avatar answered Sep 30 '22 18:09

Jason Punyon