Changing the name just because of Null?

api, c#

Solution

An API should be designed so that it's easy to use correctly and hard to use incorrectly. Your API is easy to use correctly:

AddMode(new TypeA());

does compile.

It's harder to use incorrectly:

AddMode(null);

does not compile. The user ist forced to do something like

AddMode((TypeA)null);

Which should make him think, whether this is expected usage. So I think your API is OK as it is.

Problem

I work on developing an external API. I added a method to my public interface : ``` public void AddMode(TypeA mode); public void AddMode(TypeB mode); // the new method, TypeB and TypeA are not related at all ``` It looked good, until one test broke that was passing a `null` . That made the compiler confused with ambiguous call. I fixed the test with casting the null. However my question is : - Should I change the name just because of this? - Or should let the client do the cast as I did? (if they pass null for whatever reason) What is the best in this case while designing APIs ? Edit : the call was like this AddMode(null) , not like : ``` TypeA vl = null; AddMode(v1); // this doesn't cause a problem ```

Original source

Related problems