Topic Overview

Welcome to the world of refactoring! We're learning about Code Smells, which are patterns in code that hint at potential problems. Our mission is to help you spot these smells and understand how to improve them through refactoring. We'll delve into the concept of Code Smell, examine different types, and apply real-world code examples to solidify your understanding. Let's get started!

Introduction to Code Smells

Code smells indicate something might be off within your code, akin to a warning that the code may not be as readable, efficient, or manageable as it could be.

Consider this bit of code:

double calculate(int quantity, double price) {
    return quantity * price;
}

double total = calculate(5, 3.0);

The function name calculate is too vague. What exactly does it calculate? For whom? This ambiguity is a sign of a 'bad naming' code smell.

Duplicate Code

If you notice the same piece of code in more than one place, you may be looking at an example of the Duplicate Code smell. Duplicate code leaves room for errors and bugs. If you need to make a change, you might overlook one instance of duplication.

Here's a simple example:

double total_apples_price = quantity_apples * price_apple - 5;
double total_bananas_price = quantity_bananas * price_banana - 5;

This code performs the same operation on different data. Instead of duplicating the operation, we can create a function to handle it:

double calculate_price(int quantity, double price) {
    double discount = 5.0;
    return quantity * price - discount;
}

double total_apples_price = calculate_price(quantity_apples, price_apple);
double total_bananas_price = calculate_price(quantity_bananas, price_banana);

With this solution, if we need to change the discount or the formula, we can do so in one place: the calculate_price function.

Too Long Method

A method that does too many things or is too long is harder to read and understand, making it a prime candidate for the Too Long Method smell.

Consider this example:

class Order {
public:
    std::string payment_type;
    bool is_valid() const { /* validation logic */ return true; }
    // Other order-related methods and attributes
};

bool process_order(Order& order) {
    std::cout << "Processing order..." << std::endl;
    if (order.is_valid()) {
        std::cout << "Order is valid" << std::endl;
        if (order.payment_type == "credit_card") {
            process_credit_card_payment(order);
            send_order_confirmation_email(order);
        } else if (order.payment_type == "paypal") {
            process_paypal_payment(order);
            send_order_confirmation_email(order);
        } else if (order.payment_type == "bank_transfer") {
            process_bank_transfer_payment(order);
            send_order_confirmation_email(order);
        } else {
            std::cout << "Unsupported payment type" << std::endl;
            return false;
        }
        std::cout << "Order processed successfully!" << std::endl;
        return true;
    } else {
        std::cout << "Invalid order" << std::endl;
        return false;
    }
}

This function handles too many aspects of order processing, suggesting a 'Too Long Method' smell. A better approach could involve breaking down the functionality into smaller, more focused methods and using a cleaner structure for handling payment types, avoiding strings altogether.

Here's the refactored version:

enum class PaymentType {
    CreditCard,
    PayPal,
    BankTransfer,
    Unsupported
};

class Order {
public:
    PaymentType payment_type;
    bool is_valid() const { /* validation logic */ return true; }
    // Other order-related methods and attributes
};

bool process_payment(PaymentType payment_type, Order& order) {
    switch (payment_type) {
        case PaymentType::CreditCard:
            process_credit_card_payment(order);
            break;
        case PaymentType::PayPal:
            process_paypal_payment(order);
            break;
        case PaymentType::BankTransfer:
            process_bank_transfer_payment(order);
            break;
        default:
            std::cout << "Unsupported payment type" << std::endl;
            return false;
    }
    return true;
}

bool process_order(Order& order) {
    std::cout << "Processing order..." << std::endl;
    if (!order.is_valid()) {
        std::cout << "Invalid order" << std::endl;
        return false;
    }
    
    if (process_payment(order.payment_type, order)) {
        send_order_confirmation_email(order);
        std::cout << "Order processed successfully!" << std::endl;
        return true;
    } else {
        return false;
    }
}

The original process_order function was overly long, handling order validation, payment processing, and email sending within a single block. In the refactored version, functionality is divided into smaller functions, such as process_payment, which uses the PaymentType enum and a switch-case structure. This separation clarifies responsibilities and simplifies the logic, transforming the code into more readable, manageable units. The resulting code is more modular, easier to maintain, and focused on single responsibilities, effectively addressing the initial 'Too Long Method' smell.

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