Make Your Code Clear: C#: Listing 1: Numeric library version 1

This version of the numeric library works correctly. However, there are many poor practices in this library that make it harder to use. In addition, those practices will confuse users of the code. They'll make assumptions about what the code does because of how it's structured. In its current form, those assumptions will be wrong.

public class NumericAlgorithm
{
    public double Mean(List<double> sequence)
    {
        double total = Sum(sequence);
        return (sequence.Count == 0) 
           ? total : total / sequence.Count;
    }

    public double Median(List<double> sequence)
    {
        if (sequence == null)
            throw new ArgumentNullException("sequence", 
                "Sequence must not be null");
        if (sequence.Count == 0)
            return 0;

        List<double> local = new List<double>(sequence);
        local.Sort();

        return local[local.Count / 2];
    }

    public double Variance(List<double> sequence)
    {
        double sum = Sum(sequence);
        if (sequence.Count == 0)
            return 0;
        List<double> squares = 
            sequence.Select(n => n * n).ToList();
        double sumSquares = Sum(squares);
        double variance = (sumSquares + sum * sum / 
            sequence.Count) / (sequence.Count - 1);
        return variance;
    }

    public double Sum(List<double> sequence)
    {
        if (sequence == null)
            throw new ArgumentNullException("sequence", 
            "Sequence must not be null");
        double total = 0;
        sequence.ForEach(num => total += num);

        return total;
    }
}
comments powered by Disqus

Featured

  • C# Slides in Usage Ranking of Programming Languages

    "The fact that C# lost three places in the ranking of language communities during the last three years is mostly explained by its slower growth compared to C/C++ and PHP."

  • Telerik UI for Blazor Updated

    Progress announced an update to its Telerik UI for Blazor components, targeting Microsoft's open source Blazor framework that lets C# coders create web apps without having to rely upon JavaScript.

  • Infragistics Unveils UI Components for Blazor

    Infragistics, specializing in third-party UI/UX controls and tools, unveiled a new offering targeting Blazor, Microsoft's red-hot open source framework that allows for C#-based web development instead of traditional mainstay JavaScript.

  • AWS Open Sources Tool for Porting .NET Framework Apps to .NET Core

    Leading cloud computing platform Amazon Web Services open sourced the it announced in July for helping users port old .NET Framework applications to the new .NET Core framework.

  • Uno Platform Ports Windows Calculator to Linux

    Uno Platform has ported the famed Windows Calculator, open sourced last year, to Linux as part of a continuing "proof point" effort to demonstrate the reach of what it describes as the sole UI offering available to target Windows, WebAssembly, iOS, macOS, Android and Linux with single-codebase applications coded in C# and XAML.

Upcoming Events