Reputation: 25
I have this two methods which basically do the same, I was wondering, if it is possible to introduce a new parameter to make these two methods one
Methods:
public string CodeAnalysisEnabled(XElement propertyGroup, string groupName)
{
var codeAnalysis = (from doc in propertyGroup?.Descendants("RunCodeAnalysis") select doc).ToArray();
if (codeAnalysis.Length == 0)
{
return groupName + ": RunCodeAnalysis is missing.";
}
var allOk = codeAnalysis.All(n => n.Value.Equals("true", StringComparison.InvariantCultureIgnoreCase));
return allOk ? null : groupName + ": RunCodeAnalysis has wrong state.";
}
public string WarningsAsErrorsEnabled(XElement propertyGroup, string groupName)
{
var codeAnalysis = (from doc in propertyGroup?.Descendants("TreatWarningsAsErrors") select doc).ToArray();
if (codeAnalysis.Length == 0)
{
return groupName + ": TreatWarningsAsErrors is missing.";
}
var allOk = codeAnalysis.All(n => n.Value.Equals("true", StringComparison.InvariantCultureIgnoreCase));
return allOk ? null : groupName + ": TreatWarningsAsErrors has wrong state.";
}
Upvotes: 1
Views: 49
Reputation: 2357
what about this?
public string CodeAnalysisEnabled(XElement propertyGroup, string groupName)
{
return Check(propertyGroup, groupName, "RunCodeAnalysis");
}
public string WarningsAsErrorsEnabled(XElement propertyGroup, string groupName)
{
return Check(propertyGroup, groupName, "TreatWarningsAsErrors");
}
private static string Check(XElement propertyGroup, string groupName, string checkedParam)
{
var codeAnalysis = (from doc in propertyGroup?.Descendants(checkedParam) select doc).ToArray();
if (codeAnalysis.Length == 0)
{
return groupName + $": {checkedParam} is missing.";
}
var allOk = codeAnalysis.All(n => n.Value.Equals("true", StringComparison.InvariantCultureIgnoreCase));
return allOk ? null : groupName + $": {checkedParam} has wrong state.";
}
Upvotes: 0
Reputation: 460098
Yes, there is only one difference which you can pass as argument:
public string MeaningfulMethodName(XElement propertyGroup, string groupName, string propertyName)
{
var codeAnalysis = (from doc in propertyGroup?.Descendants(propertyName) select doc).ToArray();
if (codeAnalysis.Length == 0)
{
return $"{groupName}: {propertyName} is missing.";
}
var allOk = codeAnalysis.All(n => n.Value.Equals("true", StringComparison.InvariantCultureIgnoreCase));
return allOk ? null : $"{groupName}: {propertyName} has wrong state.";
}
Upvotes: 2