Pull members up, push members down
The Pull Members Up refactoring allows you to move class members to a base class.
The Push Members Down refactoring helps you clean up the class hierarchy by moving class members to a subclass. The members are then relocated into the direct subclasses only.
Pull members up
Select the members to be moved to a superclass.
From the main or context menu, choose Pull Members Up dialog appears.
. TheSelect the destination object (superclass).
In the Members section, select the members you want to move.
Click Refactor to pull the selected members to their destination.
Examples
Before | After |
---|---|
class BaseClass {
int d1 = 0;
static const int d2 = 1;
};
class ChildClass: public BaseClass {
char SomeChar = 'a';
long d22;
void exFunc(int); //This method will be pulled up
};
void ChildClass::exFunc(int) {};
|
class BaseClass {
int d1 = 0;
static const int d2 = 1;
void exFunc(int);
};
class ChildClass: public BaseClass {
char SomeChar = 'a';
long d22;
};
void BaseClass::exFunc(int) {};
|
Before | After |
---|---|
@interface SampleClass:NSObject
/* This method will be pulled up */
- (int)IntSum:(int)num1 andNum2:(int)num2;
@end
@implementation SampleClass
- (int)IntSum:(int)num1 andNum2:(int)num2 {
int result = num1 = num2;
return result;
}
@end
|
@interface BaseClass : NSObject
- (int)IntSum:(int)num1 andNum2:(int)num2;
@end
@interface SampleClass : BaseClass
@end
@implementation BaseClass
- (int)IntSum:(int)num1 andNum2:(int)num2 {
int result = num1 = num2;
return result;
}
@end
|
Before | After |
---|---|
class SuperClass:
def super_method(self):
pass
class SubClassOne(SuperClass):
# This function will be pulled up
def my_method(self):
pass
|
class SuperClass:
def super_method(self):
pass
def my_method(self):
pass
class SubClassOne(SuperClass):
pass
|
Pulling up the dependent members
Let's consider the following sample of C++ code:
As you can see, there is a dependency between variables bi
and b2
. If you apply Pull Members Up refactoring to class ChildClass
and try to select b2
to be moved, and leave b1
in the class, CLion highlights the problem member in Pull Members Up dialog, as following:
Trying to proceed with extract, you will get the following warning message:
Choose Continue to ignore the problem and proceed with refactoring, or Cancel to return back and resolve it. Also you can observe the conflict in Find Tool Window.
Push members down
In the editor, open the class whose members you need to push down.
From the main or context menu, choose Push Members Down dialog displays the list of members to be pushed down.
.In the Members to be pushed down area, select the members you want to move. Note that the member at caret is already selected.
If pushing a member might cause problems, you will be notified with red highlighting. It means that, if the situation is unattended, an error will emerge after refactoring. CLion prompts you with a Problems Detected dialog, where you can opt to ignore or fix the problem.
Preview and apply changes.
Examples
Before | After |
---|---|
class BaseClass {
int d1 = 0;
static const int d2 = 1;
void exFunc(int); //This method will be pushed down
};
class ChildClass: public BaseClass {
char SomeChar = 'a';
long d22;
};
void BaseClass::exFunc(int) {};
|
class BaseClass {
int d1 = 0;
};
class ChildClass: public BaseClass {
char SomeChar = 'a';
long d22;
static const int d2 = 1;
void exFunc(int);
};
void ChildClass::exFunc(int) {};
|
Before | After |
---|---|
@interface Superclass : NSObject {
int v;
int w; //This variable will be pushed down
}
- (void)initV;
@end
@implementation Superclass
- (void)initV {
v = 20;
}
@end
@interface Subclass : Superclass
|
@interface Superclass : NSObject {
int v;
}
- (void)initV;
@end
@implementation Superclass
- (void)initV {
v = 20;
}
@end
@interface Subclass : Superclass { int w; }
|
Before | After |
---|---|
class SuperClass:
# This function will be pushed down
def super_method(self):
pass
class SubClassOne(SuperClass):
def my_method(self):
pass
class SubClassTwo(SuperClass):
def my_method(self):
pass
|
class SuperClass:
pass
class SubClassOne(SuperClass):
def my_method(self):
pass
def super_method(self):
pass
class SubClassTwo(SuperClass):
def my_method(self):
pass
def super_method(self):
pass
|
Pushing down the dependant members
Let's consider the following sample of C++ code:
As you can see, variable b2
here depends on b1
. If you apply Push Members Down refactoring to the above class and try to select b1
to be moved, and leave b2
in the base class, CLion highlights the problem member in Pull Members Down dialog, as following:
Trying to proceed with extract, you will get the following warning message:
Choose Continue to ignore the problem and proceed with refactoring, or Cancel to return and resolve it. Also, you can observe the conflict in Find Tool Window.