I have a brownfield application which has a complex method. The CC is 14. The if statements verify application settings and generate the appropriate inline SQL.
E.G. This if statement checks the settings. settings is custom code, a DTO with several bool (not bool?) properties.
string conditions = " AND (";
List<string> conditionStrings = new List<string>();
if (settings.AlwaysIncludeCommonResults && settings.SelectedCommonLabs.Count > 0)
{
string common = " (Table.Name in (";
for (int i = 0; i < settings.SelectedCommonLabs.Count; i++)
{
common += string.Format("'{0}'", settings.SelectedCommonLabs[i]);
if (i < settings.SelectedCommonLabs.Count - 1)
common += ", ";
}
common += ")) ";
conditionStrings.Add(common);
}
if (settings.AlwaysSelectLast24HoursResults)
{
string last24 = " (DateDiff(hh, Table.PerformedDtm, GetDate()) < 24) ";
conditionStrings.Add(last24);
}
I don’t know what to do to simplify this bool logic. Null coalesce?…I don’t know that it will make this any better. This pattern appears several more times in the same method. So I hope to reuse the answer several times to decrease overall CC and increase readability. What do you suggest?
UPDATED
After deciding to rip out the first verification, added further method logic.
I’d start by shrinking code a bit:
can be shrunk:
But I agree this is not very nice code to begin with.