Name change just because of Null?

I am working 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 that passed null . This made the compiler confusing with an ambiguous call. I fixed the test by typing zero.

However my question is:

  • Should I change the name just because of this?
  • Or should let the client throw like me? (if for some reason they skip zero)

What is the best thing about developing an API in this case?

Edit :

the call was like AddMode (null) , and not like:

 TypeA vl = null; AddMode(v1); // this doesn't cause a problem 
+6
source share
5 answers

The API must be designed so that it is easy to use correctly and difficult to use incorrectly. Your API is easy to use:

 AddMode(new TypeA()); 

compiles.

It is harder to use incorrectly:

 AddMode(null); 

not compiled. The user is forced to do something like

 AddMode((TypeA)null); 

Which should make him think whether this will be the expected use. Therefore, I think your API is ok as it is.

+6
source

I think it depends on how exceptional null as the value for the corresponding argument.

Compare, for example, this ArgumentNullException constructor : it is most often called when it is necessary to set an internal exception. Otherwise, this constructor is passed, which excludes the name of the illegal argument. At odd moments, the first one should be called because a custom message needs to be provided, but no internal exception is thrown (usually I do this when I throw an exception for the array / collection argument containing null but not null ). So, in this case, I need an explicit cast, and I would say that it is acceptable there.

If your methods really do the same, but null is still the usual value, you might want to add parameterless overloading for the null option (i.e. explicit casting is still possible, but users can also call without parameters).

If your methods do something slightly different and something else for null , you might consider completely rejecting null for the methods you specified and adding parameterless overload for the null case.

Update: If null in any case not acceptable (and will result in an exception), you should leave the method as it is. Apart from the purpose of testing, there should never be any situation in which a null literal will be passed to a method, as this will inevitably lead to an exception. Therefore, do not change the overload names in this case.

+1
source

Anyway, valid input for this method?

Personally, I would leave this the same as the overloads associated with AddMode, since you expect AddMode (X) and AddMode (Y) to do something related to each other.

If they are not related in any way, it is possible that the method name is changed in the order

0
source

Well, that depends on either the null value or the valid value in your API.

Unless you just do not accept it without supporting it. Therefore, even if a consumer tries to use it with null , the compiler will encounter an ambiguity problem.

0
source

If your API accepts NULL as a possible parameter value, you should specify it in the documentation and indicate that you need to quit it and write code examples to show how to do it.

However, if you do not want the user to use null values, you can change TypeA and TypeB as a struct instead of a class if its class design allows it.

0
source

Source: https://habr.com/ru/post/919016/


All Articles