Background: I'm working on an application that will manage backup generators. These generators need to be able to be "linked" together. For example, Generator B might serve as a backup for Generator A. If A fails, then the software turns B on. Or they might be linked in a load-sharing configuration, where Generator A and B run together, and share the load.
I'm making a function that will make the link, given 2 generators and the type of link desired.
public void LinkGenerators(Generator A, Generator B, Type linkType);
In writing my tests, I've come with a large number of invalid parameter configurations. My LinkGenerators
function looks like this:
public void LinkGenerators(Generator A, Generator B, Type linkType)
{
if (linkType.BaseType != typeof(Link))
{
throw new ArgumentException("linkType is not a valid link type");
}
if (linkAlreadyExistsFor(A, B))
{
throw new InvalidOperationException("Link for A and B already exists");
}
if (A.Equals(B) || B.Equals(A))
{
throw new InvalidOperationException("A and B cannot be the same generator");
}
if (A == null || B == null || linkType == null)
{
throw new ArgumentException("Cannot pass a null argument");
}
.....
//Actually make the link after making sure all the arguments are valid.
}
Most of the LinkGenerator functions consists of verifying that the parameters are good. The actual link creation takes 2 lines of code. There's a bit of business logic (verifying that link doesn't already exist and that the generators are not the same) mixed in with a bit functional logic (making sure that the linkType derives from Link
, arguments aren't null...), and makes me.......uncomfortable.
Is a long list of parameter checks an anti-pattern or a code smell? And if so, what can one do about it?
(Just to make this clear to close-voters probably misunderstanding the question: this is not a question about "coding style for conditions" like this one).