I have the following function for turning on some ports on a piece of hardware:
public Boolean SwitchOn(Int32 portNumber)
{
HttpCommandBuilder builder = new HttpCommandBuilder(_ipAddress);
builder.Command = HttpCommandBuilder.CommandType.SetPower;
String result;
if (portNumber == 1)
{
builder.Port1 = true;
result = SendCommand(builder.GenerateUrl());
if (String.IsNullOrEmpty(result))
{
return false;
}
else if (result.IndexOf("p61=1") == -1)
{
return false;
}
else
{
return true;
}
}
else if (portNumber == 2)
{
builder.Port2 = true;
result = SendCommand(builder.GenerateUrl());
if (String.IsNullOrEmpty(result))
{
return false;
}
else if (result.IndexOf("p62=1") == -1)
{
return false;
}
else
{
return true;
}
}
else if (portNumber == 3)
{
builder.Port3 = true;
result = SendCommand(builder.GenerateUrl());
if (String.IsNullOrEmpty(result))
{
return false;
}
else if (result.IndexOf("p63=1") == -1)
{
return false;
}
else
{
return true;
}
}
else if (portNumber == 4)
{
builder.Port4 = true;
result = SendCommand(builder.GenerateUrl());
if (String.IsNullOrEmpty(result))
{
return false;
}
else if (result.IndexOf("p64=1") == -1)
{
return false;
}
else
{
return true;
}
}
else
{
throw new ArgumentException("Invalid port number: " + portNumber);
}
}
It has a low maintainability index and there's a lot of copy and pasting going on and I am doing a similar, repetitive action for each port.
I know it's not a very elegant solution so I am looking for any advice and suggestions on how I could re-factor this function to improve its maintainability score.
SendCommand()
returnsnull
? \$\endgroup\$.edu
or.edu.something
email, or can send some sort of proof to them. \$\endgroup\$