Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 12, 2025

Summary

This PR addresses the issue of the extremely long and complex combine_data function in transformations.R by breaking it down into smaller, more manageable helper functions.

Changes Made

Main Achievement:

  • Reduced the main combine_data function from 355+ lines to 32 lines (91% reduction)
  • Improved readability by clearly showing the logical flow
  • Enhanced maintainability by splitting complex logic into focused helper functions
  • Reduced overall complexity while preserving all existing functionality

Helper Functions Created:

  1. .combine_data_validate_inputs() - Handles input validation and transformation setup
  2. .combine_data_prepare_data() - Manages data frame preparation and column handling
  3. .combine_data_validate_and_setup() - Performs data validation and output structure setup
  4. .combine_data_handle_unstandardized() - Special handling for unstandardized effect sizes (y)
  5. .combine_data_calculations() - Handles OR→logOR transformation, standard errors, sample sizes, and t-statistics calculations
  6. .combine_data_transform() - Manages effect size transformations by type (d, z, r, logOR, OR)
  7. .combine_data_create_output() - Creates final output with proper attributes

Before and After

Before: One monolithic 355+ line function handling all logic

combine_data <- function(...) {
  # 355+ lines of mixed logic covering:
  # - Input validation
  # - Data preparation
  # - Calculations
  # - Transformations
  # - Output creation
  # ... (extremely long and hard to follow)
}

After: Clean, readable coordinator function

combine_data <- function(...) {
  # Validate inputs and setup transformation
  transformation <- .combine_data_validate_inputs(...)
  
  # Prepare data frame  
  data_info <- .combine_data_prepare_data(...)
  
  # Validate data and prepare output structure
  setup_info <- .combine_data_validate_and_setup(...)
  
  # Handle unstandardized effect sizes
  if(!anyNA(data[,"y"])) {
    return(.combine_data_handle_unstandardized(...))
  }
  
  # Transform OR to logOR and perform calculations
  data <- .combine_data_calculations(data)
  
  # Transform effect sizes and standard errors
  data <- .combine_data_transform(data, transformation)
  
  # Prepare final output
  return(.combine_data_create_output(...))
}

Testing

  • ✅ All existing tests continue to pass (128 transformation tests + 958 total package tests)
  • ✅ No breaking changes to the external API
  • ✅ All functionality preserved exactly as before
  • ✅ No changes to function signatures or behavior

This refactoring significantly improves code maintainability and readability while ensuring complete backward compatibility.

Fixes #56.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Refactor combine_data function in transformations.R Refactor combine_data function in transformations.R into smaller, manageable components Jun 12, 2025
Copilot AI requested a review from FBartos June 12, 2025 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor combine_data function in transformations.R

2 participants