Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Long Method in ReportGenerator Class #1

Open
nalmadi opened this issue Sep 20, 2024 · 0 comments
Open

Refactor Long Method in ReportGenerator Class #1

nalmadi opened this issue Sep 20, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@nalmadi
Copy link
Owner

nalmadi commented Sep 20, 2024

Description:

The generateReport method in the ReportGenerator class has grown too long and handles multiple responsibilities, making it hard to understand and maintain. This long method performs tasks such as initializing report content, processing user data, formatting data, and saving the report to a file.

Code Snippet:

Here’s the current implementation of the generateReport method:

public void generateReport(List<User> users) {
    // Step 1: Initialize report content
    StringBuilder report = new StringBuilder();
    report.append("User Report\n");
    report.append("------------\n");

    // Step 2: Process each user
    for (User user : users) {
        // Retrieve user data
        String username = user.getUsername();
        String email = user.getEmail();
        String address = user.getAddress();

        // Perform some data processing
        String processedData = processUserData(username, email, address);

        // Format data for report
        String formattedData = formatReportData(username, email, address, processedData);

        // Append formatted data to report
        report.append(formattedData);
        report.append("\n");
    }

    // Step 3: Save the report to a file
    saveReportToFile(report.toString());
}

Problem:

The generateReport method is doing too much:

  • It initializes the report content.
  • It processes and formats user data.
  • It saves the report to a file.

This makes the method long and difficult to maintain.

Suggested Solution:

Refactor the generateReport method by breaking it down into smaller, more focused methods. For example:

  • Extract the logic for creating the report content into a new method.
  • Extract the logic for formatting user data into a separate method.
  • Keep the generateReport method focused on high-level orchestration.

Here’s an outline of the refactored solution:

  1. Create a new method createReportContent to handle report content creation.
  2. Extract user data formatting into a new method formatUserData.
  3. Keep generateReport focused on calling these methods and saving the report.

Acceptance Criteria:

  • The generateReport method is refactored into smaller methods.
  • The refactored methods are well-named and follow the Single Responsibility Principle.
  • The functionality of the generateReport method remains unchanged.
  • Code passes all existing and new unit tests.

Additional Context:

  • Ensure that the refactored methods are properly tested.
  • Review other methods in the class for potential refactoring opportunities.

References:

@nalmadi nalmadi added enhancement New feature or request good first issue Good for newcomers labels Sep 20, 2024
@nalmadi nalmadi self-assigned this Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant