SSW Foursquare

Do you know when functions are too complicated?

Last updated by Brady Stroud [SSW] about 1 month ago.See history

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

calculate code metrics
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.

code metrics report
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.

Adam Cogan
We open source. Powered by GitHub