Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find methods with most references

So I've got this large framework of code written in C# that I would like to start writing unit tests. We have some unit tests but only about 10 to 15 percent code coverage. Obviously, I would like to make my time the most useful and start writing unit tests for the methods with the most references first.

Does anyone know of a code analysis tool that will tell you which methods have the most references? By looking at it this way, I could ensure the most used code is well tested and then work backwards from there or at least get an area to focus most of my efforts on.

like image 912
Josh DeLong Avatar asked Oct 21 '25 05:10

Josh DeLong


2 Answers

Sounds like a job for NDepend.

like image 94
Jason Reis Avatar answered Oct 23 '25 18:10

Jason Reis


Just to refine a bit on how to achieve finding methods with most references with NDepend, you just have to write the following Code Query, and you instantly get the most referenced methods.

from m in Application.Methods
orderby m.NbMethodsCallingMe descending
select new { m, m.MethodsCallingMe }

Alternatively you can use the metric Method Rank (computed by applying the Google PageRank algorithm on the graph of methods' dependencies) or both method rank and # of references:

from m in Application.Methods
orderby m.Rank descending
select new { m, m.Rank, m.MethodsCallingMe }

Finding methods with most references with NDepend


On your blog post you (hype8912) wrote: If you know the CQL syntax for NDepend, you could take this farther by importing your current code coverage results into NDepend and then modifying the query to exclude methods that already have code coverage but for now this works good for me.

The code query could then look like:

from m in Application.Methods
where m.PercentageCoverage < 100
orderby m.Rank descending
select new { m, m.Rank, m.MethodsCallingMe, m.PercentageCoverage, m.NbLinesOfCodeCovered, m.NbLinesOfCodeNotCovered }
like image 33
Patrick from NDepend team Avatar answered Oct 23 '25 20:10

Patrick from NDepend team



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!