Home > Software design >  How do I return a range and a fixed value at the same time?
How do I return a range and a fixed value at the same time?

Time:09-29

How do I return a range for the arithmetic type and a fixed value for the geometric type at the same time? I know that I can't but is there a workaround?

public (double MinimumProfitPerGrid, double MaximumProfitPerGrid) CalculateProfitPerGrid()
{
    switch (_gridType)
    {
        case GridType.Arithmetic:
            var priceDifference = (_upperLimit - _lowerLimit) / _numberOfGrids;
            var maximumProfitPerGrid = (1 - FeePercentage) * priceDifference / _lowerLimit - 2 * FeePercentage;
            var minimumProfitPerGrid = _upperLimit * (1 - FeePercentage) / (_upperLimit - priceDifference) - 1 - FeePercentage;
            return (minimumProfitPerGrid, maximumProfitPerGrid);

        case GridType.Geometric:
            var priceRatio = Math.Pow(_upperLimit / _lowerLimit, 1.0 / _numberOfGrids);
            var profitGrid = (1 - FeePercentage) * priceRatio - 1 - FeePercentage;
            return profitGrid;
        
        default:
            throw new ArgumentOutOfRangeException();
    }
}

CodePudding user response:

If the caller doesn't care about which one of the branches the method takes, it would be reasonable to represent the single value as a range starting and ending at that same point:

return (profitGrid, profitGrid);

Then the caller can just treat everything this method returns as a range.

  • Related