Skip to main content
Tweeted twitter.com/StackCodeReview/status/1226385410730921986
deleted 19 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

How do I reduce the cyclomatic complexity of a method Method that filters a list parameters based on different fields?

I have this code which is a callback for Filter in WPFWPF ICollectionView. When When I run code analysiscode analysis, the cyclometic complexitycyclometic complexity is reported to be 1111. I consider this line by line comparison gives a better readability. How can I improve the cyclomatic complexity or How can I 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;
  }

How do I reduce the cyclomatic complexity of a method that filters a list parameters based on different fields?

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 How can I 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;
  }

Method that filters a list parameters based on different fields

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;
  }
Became Hot Network Question
Source Link

How do I reduce the cyclomatic complexity of a method that filters a list parameters based on different fields?

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 How can I 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;
  }