1.
Rename Variable:
// Before
int x = 10;
// After
int count = 10;
2. Extract Method:
// Before
void printDetails() {
printf("Name: John\n");
printf("Age: 30\n");
}
// After
void printName() {
printf("Name: John\n");
}
void printAge() {
printf("Age: 30\n");
}
3. Inline Temp:
// Before
int x = 5;
int y = x * 2;
// After
int y = 5 * 2;
4. Extract Variable:
// Before
printf("Result: %d\n", 5 * 2);
// After
int result = 5 * 2;
printf("Result: %d\n", result);
5. Extract Constant:
// Before
#define PI 3.14
float area = PI * radius * radius;
// After
const float PI = 3.14;
float area = PI * radius * radius;
6. Combine Functions:
// Before
void printName() {
printf("Name: John\n");
}
void printAge() {
printf("Age: 30\n");
}
// After
void printDetails() {
printf("Name: John\n");
printf("Age: 30\n");
}
7. Remove Dead Code:
// Before
int x = 5;
// int y = 10; // Dead code
// After
int x = 5;
8. Rename Function:
// Before
void displayMessage() {
printf("Hello, World!\n");
}
// After
void showMessage() {
printf("Hello, World!\n");
}
9. Introduce Parameter Object:
// Before
float calculateArea(float length, float width) {
return length * width;
}
// After
typedef struct {
float length;
float width;
} Rectangle;
float calculateArea(Rectangle rect) {
return rect.length * rect.width;
}
10. Introduce Explaining Variable:
// Before
if (x > 10 && y > 5 && z > 0) {
// Do something
}
// After
bool isXGreater = x > 10;
bool isYGreater = y > 5;
bool isZPositive = z > 0;
if (isXGreater && isYGreater && isZPositive) {
// Do something
}
11. Split Temporary Variable:
// Before
int temp = x * 2;
int result1 = temp - 5;
int result2 = temp + 5;
// After
int temp = x * 2;
int result1 = temp - 5;
int result2 = x * 2 + 5;
12. Replace Magic Number with Symbolic Constant:
// Before
if (x > 100) {
// Do something
}
// After
#define THRESHOLD 100
if (x > THRESHOLD) {
// Do something
}
13. Replace Nested Conditional with Guard Clauses:
// Before
if (x > 0) {
if (y > 0) {
// Do something
}
}
// After
if (x <= 0)
return;
if (y <= 0)
return;
// Do something
14. Replace Array with Object:
// Before
int scores[5] = {90, 85, 80, 95, 88};
int total = 0;
for (int i = 0; i < 5; i++) {
total += scores[i];
}
// After
typedef struct {
int scores[5];
} Student;
int total = 0;
for (int i = 0; i < 5; i++) {
total += student.scores[i];
}
15. Replace Magic String with Symbolic Constant:
// Before
char *status = "Active";
// After
#define STATUS_ACTIVE "Active"
char *status = STATUS_ACTIVE;
16. Extract Class:
// Before
typedef struct {
char name[20];
int age;
} Person;
// After
typedef struct {
char name[20];
} Name;
typedef struct {
Name name;
int age;
} Person;
17. Replace Type Code with Subclasses:
// Before
typedef struct {
int type;
// other fields
} Employee;
// After
typedef struct {
// common fields
} Employee;
typedef struct {
Employee employee;
// specific fields
} Manager;
typedef struct {
Employee employee;
// specific fields
} Engineer;
18. Replace Error Code with Exception:
// Before
int result = doSomething();
if (result == -1) {
printf("Error occurred\n");
}
// After
try {
doSomething();
} catch (Exception e) {
printf("Error occurred\n");
}
19. Replace Function with Command:
// Before
void doSomething() {
// code
}
// After
typedef void (*Command)();
void execute(Command command) {
command();
}
20. Replace Loop with Pipeline:
// Before
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += array[i];
}
// After
int sum = array[0] + array[1] + array[2] + array[3] + array[4];
21. Move Field:
// Before
typedef struct {
char name[20];
int age;
} Person;
// After
typedef struct {
char name[20];
} Name;
typedef struct {
Name name;
int age;
} Person;
22. Move Method:
// Before
typedef struct {
char name[20];
int age;
} Person;
void printName(Person person) {
printf("Name: %s\n", person.name);
}
// After
typedef struct {
char name[20];
} Name;
typedef struct {
Name name;
int age;
} Person;
void printName(Name name) {
printf("Name: %s\n", name.name);
}
23. Move Statements into Function:
// Before
int calculateTotal(int array[], int length) {
int total = 0;
for (int i = 0; i < length; i++) {
total += array[i];
}
return total;
}
// After
int calculateTotal(int array[], int length) {
return sumArray
(array, length);
}
int sumArray(int array[], int length) {
int total = 0;
for (int i = 0; i < length; i++) {
total += array[i];
}
return total;
}
24. Replace Constructor with Factory Method:
// Before
typedef struct {
char name[20];
int age;
} Person;
Person createPerson(char name[], int age) {
Person p;
strcpy(p.name, name);
p.age = age;
return p;
}
// After
typedef struct {
char name[20];
int age;
} Person;
Person createPerson(char name[], int age) {
Person p;
strcpy(p.name, name);
p.age = age;
return p;
}
25. Replace Magic Literal with Parameter:
// Before
int calculateArea(int length, int width) {
return length * width;
}
// After
int calculateArea(int length, int width) {
return length * width;
}
26. Parameterize Method:
// Before
float calculateArea(float radius) {
return 3.14 * radius * radius;
}
// After
float calculateArea(float radius, float pi) {
return pi * radius * radius;
}
27. Consolidate Conditional Expression:
// Before
if (x > 10 && y > 5) {
// Do something
}
// After
if (x > 10) {
if (y > 5) {
// Do something
}
}
28. Replace Loop with Recursion:
// Before
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
// After
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
29. Separate Query from Modifier:
// Before
int getTotalAndIncrement(int *total) {
*total += 10;
return *total;
}
// After
int incrementTotal(int *total) {
*total += 10;
}
int getTotal(int total) {
return total;
}
30. Encapsulate Field:
// Before
typedef struct {
int age;
} Person;
Person person;
person.age = 30;
// After
typedef struct {
int age;
} Person;
void setAge(Person *person, int age) {
person->age = age;
}
int getAge(Person *person) {
return person->age;
}
31. Encapsulate Collection:
// Before
typedef struct {
int scores[5];
} Student;
// After
#define MAX_SCORES 5
typedef struct {
int scores[MAX_SCORES];
int scoreCount;
} Student;
void addScore(Student *student, int score) {
if (student->scoreCount < MAX_SCORES) {
student->scores[student->scoreCount++] = score;
}
}
32. Replace Conditional with Polymorphism:
// Before
typedef struct {
int type;
// other fields
} Employee;
float calculateBonus(Employee *employee) {
if (employee->type == MANAGER) {
// Calculate manager bonus
} else if (employee->type == ENGINEER) {
// Calculate engineer bonus
}
}
// After
typedef struct {
// common fields
virtual float calculateBonus() = 0;
} Employee;
typedef struct {
// specific fields
} Manager;
typedef struct {
// specific fields
} Engineer;
float Manager::calculateBonus() {
// Calculate manager bonus
}
float Engineer::calculateBonus() {
// Calculate engineer bonus
}
33. Replace Subclass with Fields:
// Before
typedef struct {
int type;
// other fields
} Employee;
// After
typedef struct {
int isManager;
int isEngineer;
// other fields
} Employee;
34. Move Embellishment to Decorator:
// Before
void printMessage(char message[]) {
printf("Message: %s\n", message);
}
// After
void printMessage(char message[]) {
printf("Message: %s\n", message);
}
void printBoldMessage(char message[]) {
printf("<b>%s</b>\n", message);
}
void printItalicMessage(char message[]) {
printf("<i>%s</i>\n", message);
}
35. Replace One/Many Distinctions with Composite:
// Before
typedef struct {
int type;
// other fields
} Product;
// After
typedef struct {
// common fields
} Product;
typedef struct {
Product products[10];
int productCount;
} CompositeProduct;
36. Replace Type Code with State/Strategy:
// Before
typedef struct {
int type;
// other fields
} Employee;
// After
typedef struct {
// common fields
virtual float calculateBonus() = 0;
} Employee;
typedef struct {
Employee employee;
// specific fields
} Manager;
typedef struct {
Employee employee;
// specific fields
} Engineer;
37. Extract Superclass:
// Before
typedef struct {
char name[20];
} Name;
typedef struct {
Name name;
int age;
// other fields
} Person;
typedef struct {
Name name;
float salary;
// other fields
} Employee;
// After
typedef struct {
char name[20];
} Name;
typedef struct {
Name name;
// other fields
} Person;
typedef struct {
Person person;
int age;
} Employee;
38. Extract Subclass:
// Before
typedef struct {
int isManager;
int isEngineer;
// other fields
} Employee;
// After
typedef struct {
// common fields
} Employee;
typedef struct {
Employee employee;
// specific fields
} Manager;
typedef struct {
Employee employee;
// specific fields
} Engineer;
39. Form Template Method:
// Before
void processOrder(Order order) {
// Process order
calculateTotal(order);
applyDiscount(order);
shipOrder(order);
}
// After
void processOrder(Order order) {
// Process order
calculateTotal(order);