Skip to content

Refactoring Suggestion - Addressing Primitive Obsession #48

@lborja04

Description

@lborja04

Hi shoryaj98,

I trust this message finds you well. In my recent review of your code, I observed a potential instance of "Primitive Obsession" in the bill method, specifically with the use of primitive type codes to represent room types. I'd like to propose a refactoring using the "Replace Type Code with Class" technique.

Why Refactor?

Code Clarity: Using primitive type codes for room types can lead to confusion and makes the code less self-explanatory.
Maintainability: By replacing type codes with a dedicated class, the code becomes more maintainable and adaptable to future changes.

Refactoring Proposal:

public class Room {
    private double roomCharge;

    public Room(double roomCharge) {
        this.roomCharge = roomCharge;
    }

    public double getRoomCharge() {
        return roomCharge;
    }
}

public class BillProcessor {
    public static void bill(Room room, int roomNumber) {
        double amount = room.getRoomCharge();
        // ... rest of the billing logic
    }
}

// Usage:
Room luxuryDoubleRoom = new Room(4000);
Room deluxeDoubleRoom = new Room(3000);
// ... create instances for other room types

BillProcessor.bill(luxuryDoubleRoom, roomNumber);

In this refactoring, I did a rework to the Room class that encapsulates the behavior related to room types. Each room type is represented by an instance of the Room class with its specific room charge.

Feel free to consider this suggestion and adapt it to your specific needs. If you have any questions or need further clarification, please don't hesitate to reach out.

Best regards,
[Your Name]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions