Overload methods or not?

I have two methods (killZombie) that handle cases where you have one argument (string) or multiple arguments (string []). Since they do the same, I made another method called "killAZombie", which is used by two other methods. The problem I am facing is that the killAombie method is named ... somehow weird. Is this a problem that other people face? What is the best way to solve this and call my method “KillAZombie” something else that more clearly distinguishes itself from “killZombie”

public void killZombie(string zombieLocation){ killAZombie(zombieLocation); } public void killZombie(string[] zombieLocations){ foreach(string zombieLocation in zombieLocations){ killAZombie(zombieLocation); } } public void killAZombie(string zombieLocation){ //Kills a zombie at specified location } 

Another way I can solve this problem is to use two different methods instead of overloading "killZombie":

 public void killZombie(string zombieLocation){ //Kills a zombie at specified location } public void killZombies(string[] zombieLocations){ foreach(string zombieLocation in zombieLocations){ killZombie(zombieLocation); } } 

Thus, we have only two methods that are easier to understand, but then the method is not overloaded. In my opinion, it seems that having overloaded methods is good (it just means that there are fewer methods, less clutter), so I'm not sure about this solution. I would be interested to hear what would be the best way to solve this problem, thanks!

Application:

My method actually takes 4 arguments, so the parameters will be at the end. The params variable is the most important, so including it as the last argument to make params work seems rather awkward. Is my concern about putting the most important argument last, legitimate enough to split the methods into KillZombie and KillZombies, or is parameters still the right way to do something?

+4
source share
6 answers

The last of your two options is probably preferable in this case (given that assigning a function means that it works on one zombie.

However, you can also look at the params keyword to let you know what your options are. For example, if you called your function simply Kill (and if it made sense in this context), you could:

 public void Kill(params string[] zombieNames) { foreach(string name in zombieNames) { } } 

And you could call it in several ways:

 Kill("Zoey"); Kill("Francis", "Zoey"); string[] survivors = { "Zoey", "Francis", "Bill", "Louis" }; Kill(names); 

(Assuming, of course, that your survivors all turned into zombies!)

In addition, C # stylistic code typically uses pascal for function names ( KillAZombie , not KillAZombie ).

Edit to add

Yes, the order of the parameters - as long as it has no technical significance - is an important consideration in the design of the API, so if you intend to use the "less important" parameters, you probably have to do without params .

With that said, I will stick to my initial recommendation: since the function is called ( KillZombie versus Kill ), I would stick with two versions just to ensure that your name matches the parameters. I would also suggest that the user specify an IEnumerable<string> instead of an array. This will allow the developer to pass names using everything that implements IEnumerable<string> , including a string array.

+4
source

Here are some ideas.

First, the C # convention for publicly available methods is to use them: "KillZombie", not "killZombie".

You can do this one way if you want. It uses a method that accepts one or more locations. The caller can simply provide a list: KillZombies(location1, location2, location3) ;

 private void KillOneZombie(string location) { ... } public void KillZombies(string location, params string[] additionalLocations) { KillOneZombie(location); if (additionalLocations == null) return; foreach(string additionalLocation in additionalLocations) KillOneZombie(additionalLocation); } 

If you want to have two methods, consider making one of them accept an IEnumerable<string> instead of an array; in this way, the caller can pass to the list, request, array, whatever.

Your second naming pattern is more standard: KillZombie and KillZombies.

The params variable is the most important, so including it as the last argument to make params work seems rather awkward. Is my concern about putting the most important argument last, legitimate enough to split the methods into KillZombie and KillZombies, or is parameters still the right way to do something?

I would think about how you expect the method to be used. Consider, for example:

 Console.WriteLine("User: {0} Score: {1}", user[i].Name, scores[i]); 

Here we explicitly expect that "params" will be used to support a variable number of arguments in the caller. No one ever does this:

 object[] results = new object[] { user[i].Name, scores[i] }; Console.WriteLine("User: {0} Score: {1}", results); 

although this is completely legal. If you expect your method to be used as Console.WriteLine, where a variable number of parameters will be passed, but the number of parameters is known at compile time, then use the parameters.

If you expect it to be used with the second template - someone has an array of locations - then do not use params; make two methods: KillZombie and KillZombies, and one of them will take IEnumerable from the strings.

+6
source

In this case, I will probably go with your second sentence. KillZombie kills one zombie; KillZombies kills several zombies.

Another option is to use one method with params argument:

 KillZombies("foo"); // kill a single zombie KillZombies("foo", "bar"); // kill multiple zombies // ... public void KillZombies(params string[] zombieLocations) { foreach (string zombieLocation in zombieLocations) { // kills a zombie at specified location } } 

(Note that the standard C # naming convention would be to use KillZombie / KillZombies with an uppercase "K".)

+3
source

First of all, there are more than just these two alternatives.

In particular, you can use the first method without an additional method.

 public void KillZombie(string zombieLocation){ // Implement zombie killing logic here. } public void KillZombie(string[] zombieLocations){ foreach(string zombieLocation in zombieLocations) KillZombie(zombieLocation); } 

But in this case, I would recommend using two different methods. Of course, they do similar things, but one zombie accepts, and one zombie. The method name should reflect this.

Similarly, the .NET List class has similar Add and AddRange .

+2
source

In your example, it’s sad wrong - you can also use an array of params to resolve calls like KillZombies (location1, location2, location3). Params matrices allow an unfinished number of parameters.

However, this is often done for ease of use. If you have 3 ovterloads because they are all in use, is there nothing wrong with their availability or?

Take a look at the differentString.Format methods.

0
source

how about this:

 public void killZombies(string zombieLocation, params string[] zombieLocations){ killZombie(zombieLocation); if(zombieLocations != null) { foreach(string zombieLocation in zombieLocations){ killZombie(zombieLocation); } } } 

You can pass one or several zombies.

[edit] as a comment, this update prohibits killing without zombies

0
source

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


All Articles