Calculating Standard Deviations We were discussing Standard Deviations in chat, so I decided to write a standard deviation calculator:



static double PopulationStandardDeviation(List&lt;double&gt; numberSet)
{
    double mean = numberSet.Average();

    return Math.Sqrt(numberSet.Sum(x =&gt; Math.Pow(x - mean, 2)) / numberSet.Count);
}

static double SampleStandardDeviation(List&lt;double&gt; numberSet)
{
    double mean = numberSet.Sum() / numberSet.Count;

    return Math.Sqrt(numberSet.Sum(x =&gt; Math.Pow(x - mean, 2)) / (numberSet.Count - 1));
}


How could these be improved?  Because they are both Standard Devation calculators, should I combine them into one method with a header of static double StandardDeviation(List&lt;double&gt; numberSet, bool isSample)?  Are they an optimal solution for the problem?
It's good to have 2 methods.
For the reason that @britishtea said in a comment,
which is so spot-on I'm just going to quote it verbatim:


  A method that takes a boolean value as a parameter and behaves differently depending on that value is not one method, but two methods! You should keep them separated. You couldc of coursec implement them using shared functionality, but your public API should offer two methods. :) – @britishtea


And indeed, this is how the .NET framework does it too
(See StandardDeviation() and StandardDeviationP(), thanks @200_success).

At the same time, of course don't repeat yourself.
Extract the common logic to a private helper method,
and rewrite your public methods in terms of the private one:

private static double StandardDeviation(List&lt;double&gt; numberSet, double divisor)
{
    double mean = numberSet.Average();    
    return Math.Sqrt(numberSet.Sum(x =&gt; Math.Pow(x - mean, 2)) / divisor);
}

static double PopulationStandardDeviation(List&lt;double&gt; numberSet)
{
    return StandardDeviation(numberSet, numberSet.Count);
}

static double SampleStandardDeviation(List&lt;double&gt; numberSet)
{
    return StandardDeviation(numberSet, numberSet.Count - 1);
}


Lastly, the parameter name "numberSet" is a misnomer,
suggesting a Set, when it's really a List.