4
\$\begingroup\$

I have this code which is a callback for Filter in WPF ICollectionView. When I run code analysis, the cyclometic complexity is reported to be 11. I consider this line by line comparison gives a better readability. How can I improve the cyclomatic complexity or improve this code in general?

 private bool FilterLicenseList(object item)
 {
     var license = item as LicenseEntry;
     if (!string.IsNullOrEmpty(Filter.Firm) && !license.Firm.Contains(Filter.Firm)) return false;
     if (!string.IsNullOrEmpty(Filter.RegistrationCode) && !license.RegistrationCode.Contains(Filter.RegistrationCode)) return false;
     if (!string.IsNullOrEmpty(Filter.LicenseCode) && !license.LicenseCode.Contains(Filter.LicenseCode)) return false;
     if (!string.IsNullOrEmpty(Filter.Branch) && !license.Branch.Contains(Filter.Branch)) return false;
     if (Filter.Active != null && license.Active != Filter.Active) return false;
     return true;
  }
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Your code has lots of duplication. You can make it more readable:

private bool FilterLicenseList(object item)
{
    var license = item as LicenseEntry;
    return FilterString(Filter.Firm, license.Firm)
        && FilterString(Filter.RegistrationCode, license.RegistrationCode)
        && FilterString(Filter.LicenseCode, license.LicenseCode)
        && FilterString(Filter.Branch, license.Branch)
        && FilterBoolean(Filter.Active, license.Active);
}

All you need to do now is to implement the two helper methods FilterString and FilterBoolean.

I know that even the rewritten code is visually redundant since the field name appears twice in each line of code, but removing this would be more difficult. Compared to the original code I find the rewritten code much more readable, and there is no chance that in one of the lines an exclamation mark is missing, which would be pretty easy to overlook.

\$\endgroup\$
3
  • \$\begingroup\$ yes this looks more readable, the FilterString seems to be nice Idea. But still cyclomatic complexity remains 6 (at least logically). I know many tools will point it to be a complexity of 2. Redundancy can be reduced (slightly) by using Exentsion methods, something like, Filter.Firm.ApplyFilter(license.Filter). \$\endgroup\$ Commented Jan 12, 2020 at 12:58
  • 1
    \$\begingroup\$ The remaining 6 is a flaw in the definition of "cyclomatic complexity". The code as written here is very easy to understand since in natural language it can be reduced to "all of the conditions", and all the conditions are independent of each other. This is something that the cyclomatic complexity does not take into account, but we humans do. Therefore, don't trust it blindly. :) \$\endgroup\$ Commented Jan 12, 2020 at 13:27
  • \$\begingroup\$ Thank you, makes sense. \$\endgroup\$ Commented Jan 12, 2020 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.