Do you know when functions are too complicated?

Updated by Brady Stroud [SSW] 1 year ago. See history

123
<introEmbed body={<> You should generally be looking for ways to simplify your code (e.g. removing heavily-nested case statements). As a minimum, look for the most complicated method you have and check whether it needs simplifying. In Visual Studio, there is built-in support for Cyclomatic Complexity analysis. </>} />
  1. Go to Analyze | Calculate Code Metrics | For Solution
Image

Figure: Launching the Code Metrics tool within Visual Studio

  1. Look at the function with the largest Cyclomatic Complexity number and consider refactoring to make it simpler.
Image

Figure: Results from cyclomatic analysis (and other analyses) give an indication of how complicated functions are

Tip: Aim for "green" against each function's Maintainability Index.

Acknowledgements

Adam Cogan
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star