What I have here are two methods (killZombie) that handle cases where you have one argument (string) or more than one argument (string[]). Because they do the same thing, I made another method named “killAZombie” that is used by the other two methods. The problem I’m having is that the method “killAZombie” is named… well kind of strangely. Is this a problem that other people encounter too? What is the best way to solve this and name my “KillAZombie” method something else that distinguishes itself more clearly 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 see this problem being solved is by instead of overloading “killZombie” have two different methods like this:
public void killZombie(string zombieLocation){
//Kills a zombie at specified location
}
public void killZombies(string[] zombieLocations){
foreach(string zombieLocation in zombieLocations){
killZombie(zombieLocation);
}
}
This way we only have two methods that are easier to understand, but then the method isn’t overloaded. In my mind, it seems like a good thing to have overloaded methods (this just means there are fewer methods, less clutter) so I’m not sure about this solution either. I’d be interested in hearing what would be the best way to tackle this problem, thanks!
Addendum:
My method actually takes 4 arguments, so the params will be at the end. The params variable is the most important one, so putting it as the last argument to make the params work seems kind of clunky. Is my concern over putting the most important argument last, legitimate enough to split up the methods into KillZombie and KillZombies or is the params still the right way to do things?
The latter of your two choices is probably preferable in this case (given that the naming of the function implies that it’s operating upon a single “zombie”.
However, you might also want to look into the
paramskeyword, just so you know what your options are. For instance, if you’d named your function simplyKill(and if it made sense to do so in this context), you could have:And you could call it a number of ways:
(Assuming, of course, that your survivors had all been turned into zombies!)
Also, stylistically C# code generally uses pascal casing for function names (
KillAZombierather thankillAZombie).Edit for Addendum
Yes, parameter ordering–while it has no technical relevance–is an important consideration in API design, so if you’re going to be taking “less important” parameters, then you’ll probably have to do without
params.With that said, I’ll stand by my original recommendation: as the function is named (
KillZombieversusKill), I would stick with two versions just for the sake of making your name consistent with the parameters. I would also suggest allowing the user to specifyIEnumerable<string>instead of an array. That will allow the developer to pass the names using anything that implementsIEnumerable<string>, including a string array.