What’s Wrong With This Code? (#26)

Monday, January 24, 2011

A measurement consists of a high reading and a low reading, but the high and low values are optional. We also need to add measurements together, so the following C# code:

var m1 = new Measurement(lowValue: 3, highValue: 5);
var m2 = new Measurement(null, 3);
var m3 = new Measurement(6, 2);
Console.WriteLine(m1 + m2 + m3);

… should produce a total measure showing a low value of 9, a high value of 10 (effectively ignoring null values).

Unfortunately, something is wrong with the implementation...

public struct Measurement
{
    public Measurement(int? lowValue, int? highValue)
    {
        _lowValue = lowValue;
        _highValue = highValue;
    }

    public static Measurement operator +(Measurement first, 
                                         Measurement second)
    {
        var result = new Measurement
        {
            _lowValue = first._lowValue + second._lowValue,
            _highValue = first._highValue + second._highValue
        };
        return result;
    }

    public override string ToString()
    {
        return String.Format("{0}-{1}", _lowValue, _highValue);
    }

    private int? _lowValue;
    private int? _highValue;
}

Did you spot it?


Comments
gravatar Jon Monday, January 24, 2011
You can't add a null to something
Fred Monday, January 24, 2011
The code should check the nullable variables for HasValue before trying to use them in mathematical operations.
gravatar scott Monday, January 24, 2011
@jon - @Fred - correct. Too easy?
gravatar Kev Hunter Wednesday, January 26, 2011
Yeah even I got that one, maybe if you hadn't shows the calling code it would be more difficult, but the null just looks out of place!
gravatar Kev Hunter Wednesday, January 26, 2011
Yeah even I got that one, maybe if you hadn't shows the calling code it would be more difficult, but the null just looks out of place!
gravatar Brent Wednesday, January 26, 2011
...and you don't reject new Measurement(null, null) in your constructor.
gravatar Paul Hadfield Wednesday, January 26, 2011
Just to add the MSDN link, see section on operators:

http://msdn.microsoft.com/en-us/library/2cf62fcy(v=vs.80).aspx

I thought null + 1 could end up throwing an exception, but it just nullifies the answer so you'd lose the non-null value.
Ivan Wednesday, January 26, 2011
>Too easy?
No. But the code chunk is small and there is a single place to think about (int?).
TweeZz Thursday, January 27, 2011
I found this one (very) easy compared to what you normally post :)
gravatar Amit Saraswat Thursday, January 27, 2011
The predefined unary and binary operators and any user-defined operators that exist for value types may also be used by nullable types. These operators produce a null value if any of the operands is null; otherwise, the operator uses the contained value to calculate the result, instead of throwing an exception.

Above codes produces output as a total measure showing a low value of null, a high value of 10.

To obtain desired results, code should check for null values and replace it with zero before addition.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!