user1323245
user1323245

Reputation: 638

How to handle null when overloading operator + for a class value object?

I want to have to have a value object that represents length. I would prefer to use a struct given that it is a value type, but since zero length does not make sense I am forced to use a class. Adding two lengths together seems like a reasonable operation, so I want to overload the + operator. I am curious though, how should I handle adding null?

Adding null to an existing string returns a string with the same content as the existing string. Adding null to a int? that has a value returns null.

I can see a case where adding nullto an existing length simply returns a new length with the same value as the existing length. At the same time, I can see a case where adding null would be considered a bug. I have been trying to find some guidance but have not been able to find any. Is there a common guideline for this or is it different for each application?

Upvotes: 0

Views: 69

Answers (3)

NWoodsman
NWoodsman

Reputation: 505

Strictly speaking, a null length doesn't exist in reality, everything has length. Getting a null return or a NullReferenceException when working with your struct would lead me to think I messed up the constructor or instantiation. In other words, the null reference would be employed in the scope of the application and not exposed to the client.

struct length = new MyStruct(); //no!
struct length = new MyStruct(double feet, double inches) //better...

struct length = 34.5; //ok...

Upvotes: 0

Noah Reinagel
Noah Reinagel

Reputation: 402

Simple Answer: if your allowed to add nulls in your system then you should probably keep the existing value and treat it like a 0 like so:

public static NullNumber operator+ (NullNumber b, NullNumber c) {
    return (b ?? 0) + (c ?? 0); 
}

Advanced Answer: You are probably correct about length not making sense at 0 and you are right about adding nulls seems like a bug I can't see where the field is populated but I suspect either: you don't have a constructor that requires you to pass in a length if it's required. Or you have a faulty class that sometimes has a length and sometimes meaning it sounds closer to 2 classes

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726939

I would highly recommend using struct for your length, and treating the default representation as zero length.

since zero length does not make sense I am forced to use a class

It is up to your code to treat the default representation of length struct as a representation of some specific length. In addition to treating it as zero length, you have at least two options:

  • You can treat default length as an unknown, in which case any operation with it would produce an unknown, or
  • You can treat it as a "trap representation" of length, in which case any operation with it would produce an exception.

It is probably a design mistake to not treat zeros in a uniform way with all other numbers. Specifically, zero length may become handy when you subtract length values, because subtracting two values of equal length would have nothing to produce.

As far as "unknown" length is concerned, using struct gives you a convenient standard representation of Nullable<length> immediately familiar to users of your length structure.

Upvotes: 1

Related Questions