Home > Code > NDepend your guide to Kaizen your codebase

NDepend your guide to Kaizen your codebase

From time to time I have used a tool called NDepend to help me analyze the codebase I have been working on. Most of the times in the past I have been working in codebases where I was part of the project from the beginning or near its inception. This time I get to work on a codebase that has been developed for a few years and most of the people who started the development is long gone.

CQL the Sensei that let you find your way

When working with code I always try to follow “The Boy Scout Rule” and leave the code in a better state than when I started. I usually do this by refactoring and cleaning up when I touch the code for specific reason like a change request or bug fix. But some times you gets some slack or have quality improvement time scheduled. When this happens how do you know what part to go work on first? You need someone to guide you.

This is the area where NDepend really shines. NDepend really is like a Sensei that shows you the way. There are the standard reports and you get things like:

  • Methods with the most lines of code
  • Methods with the highest cyclomatic complexity
  • Methods with the most parameters
  • Methods with too many local variables

And the list goes on and on. The amazing and greatest thing however is that theses reports are not static. The reports are built with CQL – Code Query Language. This is more or less like having a SQL database that you can query, but the database is your codebase! In my case I needed to modify the most lines of code query to exclude the “InitializeComponent” method to see what methods to look into.

Here is the CQL query I used and the result(method names renamed to protect the guilty Smile ):

SELECT TOP 10 METHODS  
 WHERE !(NameLike “InitializeComponent”)  
ORDER BY NbLinesOfCode DESC  
   
methods # lines of code (LOC)
MethodName1 424
MethodName2 401
MethodName3 383
MethodName4 340
MethodName5 303
MethodName6 301
MethodName7 292
MethodName8 265
MethodName9 262
MethodName10 257
Sum: 3 228
Average: 322.8
Minimum: 257
Maximum: 424
Standard deviation: 57.844
Variance: 3 345

Here is one of the standard queries that gives me the most complex methods.

// <Name>Methods too complex (CyclomaticComplexity)</Name>
WARN IF Count > 0 IN SELECT TOP 10 METHODS WHERE 
  CyclomaticComplexity > 20 
  ORDER BY CyclomaticComplexity DESC
 
methods Cyclomatic Complexity (CC) Full Name
Method1 147 Assembly.Class.Method1
Method2 125 Assembly.Class.Method2
Method3 119 Assembly.Class.Method3
Method4 118 Assembly.Class.Method4
Method5 107 Assembly.Class.Method5
Method6 103 Assembly.Class.Method6
Method7 92 Assembly.Class.Method7
Method8 91 Assembly.Class.Method8
Method9 88 Assembly.Class.Method9
Method10 88 Assembly.Class.Method10
Sum: 1 078  
Average: 107.8  
Minimum: 88  
Maximum: 147  
Standard deviation: 18.443  
Variance: 340.16  

With the standard reports and CQL I have Sensei that guides me in my Kaizen work on the codebase.

Visualize your codebase

Queries and tables with the results are very useful but with NDepend you can go to the next level by visualizing you codebase.

MetricTreemapSnapshot

This visualization shows the number of lines of code per method in all classes analyzed. It is very easy to spot the problem areas in the codebase.

AbstractnessVSInstability 

This is another diagram that help me spot potential problem areas of the codebase.

NDepend is really a great tool to give you a feel for your codebase and where your focus areas for you code Kaizen should be. Give it a try.  

Two previous post related to this topic:

About these ads
  1. mostofa
    2010-12-06 at 00:25 | #1

    Hi Håkan,

    It seems that you have put in a lot of effort for using this tool. I am just wondering how much time we actually get during a sprint for such a high level of refactoring .

    How much time did it take for you ? Was this refactoring planned beforehand ? How did you convince the stakeholders that this was important ? :)

    /Mostofa

    • 2010-12-06 at 00:37 | #2

      Time spend doing the analysis is like doing a compilation of your code. Doing the actual refactoring is an ongoing work. Whenever I have some down time I can use the reports to guide me to some place in the codebase that needs my love. Whenever I touch some existing code I try to make it better.

      I no longer try to get the non-technical stakeholders to give me this time. I just do it. It’s part of being a professional developer.

  2. mostofa
    2010-12-06 at 00:56 | #3

    I really like the way you care for your code :) .

    • 2010-12-06 at 00:58 | #4

      If you care about your code it is more fun and you will have fewer bugs, just because you care.

  3. 2010-12-06 at 09:58 | #5

    Håkan, have you seen that now NDepend comes with new reports (the report screenshot shows older style report)
    http://www.ndepend.com/SampleReports.aspx

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 326 other followers

%d bloggers like this: