Skip to content

Refactoring Suggestion - Addressing Long Method #49

@lborja04

Description

@lborja04

Hi shouryaj98,

I trust this message finds you well. During my recent code review, I noticed a potential improvement opportunity in the bookroom method. It exhibits characteristics of a "Long Method," which can impact code readability and maintainability. I'd like to propose a refactoring using the "Preserve Whole Object" technique.

Why Refactor?

Readability: Long methods can be challenging to read and understand. Breaking them into smaller, focused methods enhances code readability.
Maintainability: Smaller methods are easier to maintain and update, promoting a more agile development process.
Refactoring Proposal:

public class RoomBooking {
    private static Scanner sc = new Scanner(System.in);

    public static void bookroom(int roomType) {
        int roomNumber = displayAvailableRooms(roomType);

        if (roomNumber != -1) {
            try {
                CustDetails(roomType, roomNumber);
                System.out.println("Room Booked");
            } catch (Exception e) {
                System.out.println("Invalid Option");
            }
        } else {
            System.out.println("Enter valid option");
        }
    }

    private static int displayAvailableRooms(int roomType) {
        int roomNumberOffset = 0;
        Room[] rooms;

        switch (roomType) {
            case 1:
                rooms = hotel_ob.luxury_doublerrom;
                break;
            case 2:
                rooms = hotel_ob.deluxe_doublerrom;
                roomNumberOffset = 10;
                break;
            case 3:
                rooms = hotel_ob.luxury_singleerrom;
                roomNumberOffset = 30;
                break;
            case 4:
                rooms = hotel_ob.deluxe_singleerrom;
                roomNumberOffset = 40;
                break;
            default:
                return -1;
        }

        System.out.println("\nChoose room number from : ");
        for (int j = 0; j < rooms.length; j++) {
            if (rooms[j] == null) {
                System.out.print(j + roomNumberOffset + ",");
            }
        }

        System.out.print("\nEnter room number: ");
        return sc.nextInt() - roomNumberOffset;
    }
}

By breaking down the bookroom method into smaller, focused methods, we improve the overall readability and maintainability of the code. Each method now has a clear and specific responsibility.

Feel free to incorporate this suggestion, and if you have any questions or need further assistance, please don't hesitate to reach out.

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