Stepping into Refactoring Code

Welcome to our captivating session on refactoring, a powerful tool for tidying up code, much like organizing a toolbox for efficiency.

In C++, each line of code is akin to a foundational component in a complex system, and poorly organized code can lead to unwieldy and unstable software. Today, we'll focus on enhancing the readability, maintainability, and performance of our code through refactoring.

Recapping Crucial Concepts

Let's briefly revisit a few key concepts:

  • Code Smells: Indicators that our code needs refactoring, such as tangled dependencies or redundancy that call for cleanup.

  • Refactoring Techniques: We've familiarized ourselves with Extract Method and Rename Method techniques in earlier lessons, using C++ functions and member functions to maintain clarity and abstraction.

  • OOP in Refactoring: We've harnessed Object-Oriented Programming principles to enhance our code's structure.

  • Code Decoupling and Modularization: Methods to manage code by minimizing dependencies while organizing through header and implementation files.

We'll use these concepts as guiding stars as we journey through the refactoring process in C++.

Practice Problem 1: Taming a Complex Function

Let's begin by refactoring a complex game score computation function. Observe the initial scenario in C++:

int computeScore(const Player& player, const std::vector<int>& monsters) {
    int score = 0;
    for (const auto& monster : monsters) {
        if (player.getPower() > monster) {
            score += player.getPower() - monster;
        } else {
            score -= player.getPower() - monster;
        }
    }
    return score;
}

Here, the repetition of the player.getPower() > monster and player.getPower() - monster calls suggests an opportunity for refactoring. We'll apply the Extract Method and Rename Method to improve this:

  • We'll extract the scoring logic into a separate function, calculateScoreChange.
  • We'll rename the original function to computeGameScore.

The refactored code will enhance clarity and modifiability:

// New function to calculate score changes.
int calculateScoreChange(int power, int monster) {
    return (power > monster) ? (power - monster) : (monster - power);
}

// Refactored function to calculate the game score.
int computeGameScore(const Player& player, const std::vector<int>& monsters) {
    int score = 0;
    for (const auto& monster : monsters) {
        score += calculateScoreChange(player.getPower(), monster);
    }
    return score;
}

This refactoring makes the code easier to understand and adapt for future changes.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal