Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Refactoring techniques for cleaner code

March 26, 2023

Refactoring Techniques for Cleaner Code

As developers, we often find ourselves working with code that is messy, hard to understand, and difficult to maintain. In these situations, refactoring comes to the rescue. Refactoring is the process of restructuring existing code without changing its external behavior. This allows us to improve the code’s readability, maintainability, and flexibility, making it cleaner and easier to work with. In this article, we’ll explore some common refactoring techniques and demonstrate their usage with examples in C and Python.

1. Extract Method/Function

One of the most common refactoring techniques is extracting a block of code into a separate method or function. This improves readability and reusability by breaking down complex logic into smaller, more understandable pieces.

C Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
void processOrder(Order* order) {
    validateOrder(order);
    calculateTotal(order);
    updateInventory(order);
}

void validateOrder(Order* order) {
    // validation logic
}

void calculateTotal(Order* order) {
    // calculation logic
}

void updateInventory(Order* order) {
    // inventory update logic
}

Python Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def process_order(order):
    validate_order(order)
    calculate_total(order)
    update_inventory(order)

def validate_order(order):
    # validation logic

def calculate_total(order):
    # calculation logic

def update_inventory(order):
    # inventory update logic

2. Rename Variable/Function

Choosing meaningful and descriptive names for variables and functions is crucial for code readability. When the existing names are unclear or misleading, it’s time to refactor and give them more appropriate names.

C Example:

1
2
int x = 10; // unclear
int numberOfStudents = 10; // descriptive

Python Example:

1
2
x = 10 # unclear
number_of_students = 10 # descriptive

3. Replace Magic Numbers with Constants

Magic numbers are hard-coded literal values in the code that can be confusing and reduce code maintainability. Refactoring by replacing them with named constants or enums improves code readability and makes it easier to modify the values later.

C Example:

1
2
#define MAX_STUDENTS 100
int students[MAX_STUDENTS];

Python Example:

1
2
MAX_STUDENTS = 100
students = [None] * MAX_STUDENTS

4. Remove Duplicate Code

Duplication in code not only increases the risk of introducing bugs but also makes the code harder to maintain. Refactoring to remove duplicated code and replacing it with a single, reusable piece helps in improving code quality and reducing redundancy.

C Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
void processInput(int data) {
    // common logic
    // ...
}

void processUserInput(int userInput) {
    processInput(userInput);
}

void processSystemInput(int systemInput) {
    processInput(systemInput);
}

Python Example:

1
2
3
4
5
6
7
8
9
def process_input(data):
    # common logic
    # ...

def process_user_input(user_input):
    process_input(user_input)

def process_system_input(system_input):
    process_input(system_input)

Conclusion

Refactoring is an essential practice for maintaining clean, readable, and maintainable code. By applying the described techniques and principles, we can continuously improve the quality of our code to make it more elegant, efficient, and easy to work with.


➡️ Simple command line interface library


⬅️ The Open/Closed Principle: What it is and why it matters


Go back to Posts.