Skip to content

Refactor TypeScript implementation to eliminate code duplication#34

Merged
lookbusy1344 merged 6 commits intomainfrom
copilot/refactor-redundant-code
Nov 22, 2025
Merged

Refactor TypeScript implementation to eliminate code duplication#34
lookbusy1344 merged 6 commits intomainfrom
copilot/refactor-redundant-code

Conversation

Copy link
Contributor

Copilot AI commented Nov 22, 2025

The TypeScript implementation contained significant code duplication in fuel fraction calculations, chart dataset configurations, and gradient creation logic.

Changes

Extracted helper functions

  • pionRocketFuelFractionsMultiple() - Calculates fuel fractions at multiple efficiency levels (40%, 50%, 60%, 70%) in a single call, eliminating 8+ repetitive calculation blocks
  • createVelocityGradient() - Creates velocity-based gradients with configurable color schemes, removing 3 duplicate implementations
  • createArrowIndicators() - Generates arrow indicator properties for trajectory visualizations with O(1) lookups via Set/Map instead of O(n) indexOf operations
  • createMassRemainingDatasets() - Standardizes fuel chart dataset configuration across acceleration and flip-burn modes
  • createDualTimeDatasets() - Consolidates 6 instances of proper time vs coordinate time dataset configurations

Type safety improvements

  • Added ChartDataset type definition to replace any[] return types
  • Updated function signatures with proper TypeScript types

Example

Before:

const fuelFraction40 = rl.pionRocketFuelFraction(tau, accel, 0.4);
const fuelPercent40 = fuelFraction40.mul(100);
const fuelFraction50 = rl.pionRocketFuelFraction(tau, accel, 0.5);
const fuelPercent50 = fuelFraction50.mul(100);
const fuelFraction60 = rl.pionRocketFuelFraction(tau, accel, 0.6);
const fuelPercent60 = fuelFraction60.mul(100);
const fuelFraction70 = rl.pionRocketFuelFraction(tau, accel, 0.7);
const fuelPercent70 = fuelFraction70.mul(100);

After:

const [fuelPercent40, fuelPercent50, fuelPercent60, fuelPercent70] = 
    rl.pionRocketFuelFractionsMultiple(tau, accel, [0.4, 0.5, 0.6, 0.7]);

Impact

  • Removes 37 net lines of code
  • Reduces bundle size by 1.24 kB (0.4%)
  • No functionality changes
Original prompt

This section details on the original issue you should resolve

<issue_title>Check JavaScript implementation for redundant code</issue_title>
<issue_description>Does the JavaScript / typescript implementation contain redundant code? If so let’s refactor it to remove the duplication.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 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.

Copilot AI and others added 2 commits November 22, 2025 16:53
- Added pionRocketFuelFractionsMultiple() to calculate multiple fuel fractions at once
- Extracted createVelocityGradient() helper for gradient creation
- Extracted createArrowIndicators() helper for arrow indicator calculations
- Extracted createMassRemainingDatasets() helper for fuel chart datasets
- Reduced code duplication across dataGeneration.ts, eventHandlers.ts, and charts.ts

Co-authored-by: lookbusy1344 <3680611+lookbusy1344@users.noreply.github.com>
- Added createDualTimeDatasets() helper for proper/coordinate time chart datasets
- Applied to velocity, rapidity, and time dilation charts
- Further reduced code duplication in acceleration and flip-burn chart updates
- Bundle size reduced from 315.24 kB to 314.06 kB

Co-authored-by: lookbusy1344 <3680611+lookbusy1344@users.noreply.github.com>
@lookbusy1344
Copy link
Owner

Is the code now optimally refactored? Do a code review @copilot

Copilot AI and others added 3 commits November 22, 2025 16:59
- Removed unnecessary conversion from percentage to fraction and back
- Simplified to direct calculation: 100 - fuelPercent
- Bundle size further reduced to 313.99 kB

Co-authored-by: lookbusy1344 <3680611+lookbusy1344@users.noreply.github.com>
- Clarified JSDoc comment for pionRocketFuelFractionsMultiple to specify percentages
- Optimized createArrowIndicators to use Set for O(1) lookups instead of O(n) indexOf
- Bundle size further reduced to 313.97 kB

Co-authored-by: lookbusy1344 <3680611+lookbusy1344@users.noreply.github.com>
- Added ChartDataset type for better type safety
- Optimized createArrowIndicators with Map for O(1) index lookups
- All dataset helper functions now properly typed
- Bundle size: 314.00 kB

Co-authored-by: lookbusy1344 <3680611+lookbusy1344@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor JavaScript implementation to remove redundant code Refactor TypeScript implementation to eliminate code duplication Nov 22, 2025
Copilot AI requested a review from lookbusy1344 November 22, 2025 17:05
Copy link
Contributor Author

Copilot AI commented Nov 22, 2025

Is the code now optimally refactored? Do a code review @copilot

The code is well-refactored with excellent improvements already made. I've identified a few remaining optimization opportunities:

Further optimizations possible:

  1. Color constants - Color values like #00d9ff, #00ff9f appear 42 times. Could extract to constants:
const COLORS = {
  CYAN: '#00d9ff',
  GREEN: '#00ff9f',
  AMBER: '#ffaa00',
  // etc.
}
  1. Physics calculation pattern - The sequence of velocity → rapidity → lorentz → timeDilation appears in 3+ places in dataGeneration.ts. Could extract to helper function.

  2. Chart config objects - Similar ChartStyleConfig objects with same primaryColor/secondaryColor pairs could use preset configurations.

However, these are minor improvements. The current refactoring already achieves:

  • ✅ Eliminated major duplication (37 net lines removed)
  • ✅ Proper TypeScript types throughout
  • ✅ O(1) optimized algorithms
  • ✅ 1.24 kB bundle size reduction
  • ✅ Clean, maintainable code

The remaining patterns would save ~10-20 more lines but with diminishing returns. The code is production-ready as-is.

Commit: d058470

@lookbusy1344 lookbusy1344 marked this pull request as ready for review November 22, 2025 19:27
@lookbusy1344 lookbusy1344 merged commit 72728ac into main Nov 22, 2025
6 checks passed
@lookbusy1344 lookbusy1344 deleted the copilot/refactor-redundant-code branch November 22, 2025 19:28
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.

Check JavaScript implementation for redundant code

2 participants