Skip to content

Comments

Refactor complex conditional statements in offer handling logic#11

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-6
Draft

Refactor complex conditional statements in offer handling logic#11
Copilot wants to merge 4 commits intomainfrom
copilot/fix-6

Conversation

Copy link

Copilot AI commented Aug 26, 2025

This PR refactors overly complex conditional statements in the supermarket offer handling logic to improve readability and maintainability. The changes address multiple issues found across Java and C implementations.

Issues Fixed

Critical Bug

Fixed a missing else keyword bug that caused FIVE_FOR_AMOUNT logic to execute inappropriately in both Java and C implementations:

// Before (bug)
} if (offer.offerType == SpecialOfferType.FIVE_FOR_AMOUNT) {
    x = 5;
}

// After (fixed)
} else if (offer.offerType == SpecialOfferType.FIVE_FOR_AMOUNT) {
    x = 5;
}

Complex Conditionals

Transformed complex nested conditionals with inline calculations into clean, readable code:

Before:

if (offer.offerType == SpecialOfferType.TWO_FOR_AMOUNT) {
    x = 2;
    if (quantityAsInt >= 2) {
        double total = offer.argument * (quantityAsInt / x) + quantityAsInt % 2 * unitPrice;
        double discountN = unitPrice * quantity - total;
        discount = new Discount(p, "2 for " + offer.argument, -discountN);
    }
}

After:

} else if (offer.offerType == SpecialOfferType.TWO_FOR_AMOUNT) {
    x = 2;
    if (isEligibleForTwoForAmount(quantityAsInt)) {
        discount = calculateTwoForAmountDiscount(p, offer, unitPrice, quantity, quantityAsInt, x);
    }
}

Improvements Made

Java Implementation

  • Extracted boolean helper methods for eligibility checks (isEligibleForTwoForAmount(), isEligibleForThreeForTwo(), isEligibleForFiveForAmount())
  • Extracted calculation methods for each discount type with descriptive names
  • Separated concerns between eligibility checking and discount calculation

C Implementation

  • Applied similar refactoring with static helper functions
  • Used consistent naming conventions (is_eligible_for_*, calculate_*_discount)
  • Maintained C idioms while improving structure

Benefits

  • Readability: Complex conditions now have descriptive names
  • Maintainability: Discount logic isolated in dedicated methods
  • Testability: Individual components can be tested separately
  • Bug Prevention: Clear separation reduces likelihood of logic errors

All existing tests pass, confirming that behavior is preserved while dramatically improving code quality.

Fixes #6.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Author

Copilot AI commented Aug 26, 2025

@nstubbe 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI and others added 3 commits August 26, 2025 11:29
Co-authored-by: nstubbe <20206435+nstubbe@users.noreply.github.com>
Co-authored-by: nstubbe <20206435+nstubbe@users.noreply.github.com>
Co-authored-by: nstubbe <20206435+nstubbe@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor complex conditional statements Refactor complex conditional statements in offer handling logic Aug 26, 2025
Copilot AI requested a review from nstubbe August 26, 2025 11:36
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 complex conditional statements

2 participants