简化冗余变量赋值

Simplifying redundant variable assignment

我不喜欢这段代码,它过于复杂且不切实际,所以我希望对其进行简化。

我想让它随机改变一个变量,我需要在这段代码中放入至少 150 个变量。

//Variable list

public double price1 = 100;
public double price2 = 100;
public double price3 = 100;

public void DaysEnd(){ //Simplified version of inefficient code

var = price1;
HVariation();
price1 = newvar;

var = price2;
HVariation();
price2 = newvar;

var = price2;
MVariation();
price2 = newvar;

var = price3;
LVariation();
price3 = newvar;
}

public void Hvariation(){
    newvar = var + (var * (Random.NextDouble(0 - 0.5, 0.5)));
}
public void Mvariation(){
    newvar = var + (var * (Random.NextDouble(0 - 0.25, 0.25)));
}
public void Lvariation(){
    newvar = var + (var * (Random.NextDouble(0 - 0.1, 0.5)));
}

这应该可以帮助您入门

List<double> values = new List<double> { 100, 100, 200, 500, ... };
values = values.Select(val => Hvariation(val)).ToList();
// now all values have been altered by Hvariation

...
private readonly Random _rand = new Random();

public double Hvariation(double val) {
    return val + (val * (_rand.NextDouble(-0.5, 0.5)));
}

首先要做的是找出重复代码。例如:

var = price3;
LVariation(); //Different variations
price3 = newvar;

这可以变成一个方法(将变化作为参数)。 为此,您还需要制作一个采用最小值和最大值的默认变体:

public void Variation(double min, double max){
    newvar = var + (var * (Random.NextDouble(min, max)));
}

然后您可以将它们放在一起以减少代码,使其看起来像这样:

public double UpdatePrice(double price, double min, double max)
{
    var = price;
    Variation(min, max);
    return newvar;
}

一般来说,如果我不得不多次复制代码(如果复制的数量很大,甚至一次),我会把代码变成一个方法。

您可以通过定义三个变体方法、定义变体级别并将其传递到单个方法中来简化此过程。我不确定你是否需要它在数组中或者你是否可以使用列表(在这种情况下列表更可取),但你可以将变量存储在数组中而不是为每个变量定义一个变量名并将它们分开根据需要进行逻辑分组。然后,您可以使用 LINQ 将 change/transformation 应用于每个数组。这方面的一个例子是

    public enum VariationLevel
    {
        High,
        Medium,
        Low
    };
    public double[] HighVariancePrices =
    {
        100, 100, 100, 100, 100
    };
    public double[] MediumVariancePrices =
    {
        100, 100, 100, 100, 100
    };
    public double[] LowVariancePrices =
    {
        100, 100, 100, 100, 100
    };


    public void DaysEnd()
    {
        HighVariancePrices = HighVariancePrices.Select(price => GetVariation(price, VariationLevel.High)).ToArray();
        MediumVariancePrices = MediumVariancePrices.Select(price => GetVariation(price, VariationLevel.Medium)).ToArray();
        LowVariancePrices = LowVariancePrices.Select(price => GetVariation(price, VariationLevel.Low)).ToArray();
    }

    public double GetVariation(double value, VariationLevel variationLevel)
    {
        switch (variationLevel)
        {
            case VariationLevel.High:
                return value + (value * (Random.NextDouble(0 - 0.5, 0.5)));
            case VariationLevel.Medium:
                return value + (value * (Random.NextDouble(0 - 0.25, 0.25)));
            case VariationLevel.Low:
                return value + (value * (Random.NextDouble(0 - 0.1, 0.5)));
        }
    }

但是,Random.NextDouble() 周围的代码无法编译(因为 NextDouble 不接受参数)所以我不确定你想在那里做什么,但那不在"how can I simplify my code?" 的范围希望这对一些人有所帮助。