I have a method which contains a series of calls to a method which checks if string input is null or empty. If its empty, it sets the 2nd parameter to false;
public bool inputsContainData()
{
bool validInputs = True;
_helper.StringContainsData(_view.FilePath1, validInputs);
_helper.StringContainsData(_view.FilePath2, validInputs);
//...
Return validInputs;
}
The StringContainsData() method is in a class which is DI’d into the presenter. The method doesnt return anything since the result variable is of reference type.
public void StringContainsData(string input, bool result)
{
if( string.IsNullOrEmpty(input))
{
result = false;
}
}
Im having some issues with this because i cant mock the StringContainsData() and have it return a specific result. The only solution i can think of is if i include a second If statement as follows:
public bool StringContainsData(string input, bool result)
{
if( string.IsNullOrEmpty(input))
{
if(result != false)
{
return false;
}
}
else
{
if(result == false)
{
return false;
}
}
return true;
}
Thus stopping me from having True,False,True => final result = true rather than false.
Is there a better way of doing this?
The simplest is to restructure to chain the calls.
There is no need for another if, simply ANDing the previous answer with the new one will work.
or if you want to have some fun
Notes:
The last version looks like complication for its own sake but if you are going to add more file paths to check extending the array is simpler/safer than cut/past on the assignment/call lines.
In the function, check ret first using the short circuit (&&) means that we do not need to check the current input if we have already failed.