NDepend, a first look

After reading @DaedTech’s very persuasive arguments for static code analysis I have decided to give NDepend a go.

Download and install is slightly fiddly, you get a zip file with a number of files. Once you have found the instructions on the web site it takes just a couple of minutes to get set up.

I analysed my current project and viewed the dashboard:

Dashboard

The first thing to catch my eye is 10 critical rules violated:

Dashboard

6 Methods with too many parameters:

  • 3 * read only model constructors
  • 2 * anonymous type constructors
  • 1 * controller constructor with 10 dependencies (+)

3 Methods too complex:

  • 2 * anonymous type constructors
  • 1 * unit test with 6 lines of code – here’s one of the 4 assert statements (they are all 4 very similar)
Assert.Contains<TemplateRecipientRole>(
    result, item =>
    item.TemplateId == "TestTemplate1" &&
    item.RecipientRoleId == "TestRole1" &&
    item.OriginalEnabled.Value &&
    !item.OriginalDefaultSequence.HasValue &&
    !item.CopyEnabled.HasValue &&
    !item.CopyDefaultSequence.HasValue);

9 Avoid namespaces mutually dependent:

  • 9 * (+)

20 Potentially dead methods:

  • 4 * the constructors of a class that is missing (or does not require) a unit test
  • 5 * where the method is being called with a dynamic parameter (this.SomeMethod(((dynamic)Entity).Attribute);)
  • 3 * private methods for Elmah (void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args))
  • 1 * unit test method accidentally marked as private
  • 8 * dead methods (+)

14 Constructors of abstract classes should be declared as protected or private:

  • 9 * WCF generated code
  • 5 * (+)

4 Don’t assign a field from many methods:

  • 4 * these are all Stub classes

2 Don’t call your method Dispose:

  • 2 * class are implementing IHttpModule which defines a Dispose method

12 Avoid having different types with the same name:

  • 12 * (+)

1 Attribute class name should be suffixed with ‘Attribute’:

  • 1 * (+)

24 Interface name should begin with a ‘I’:

  • 24 * WCF generated code

So after a lot of filtering I am left with the following things I need to change:

  • 9 Avoid namespaces mutually dependent
  • 8 Potentially dead methods
  • 5 Constructors of abstract classes should be declared as protected or private
  • 12 Avoid having different types with the same name
  • 1 Attribute class name should be suffixed with ‘Attribute’

From an initial list of 95 Critical violations NDepend has correctly identified 35 violations that I can put right. And this is all good information that would clean up my code and I wish that NDepend has been a step in the CI build from day 1.


So, onto 69 Rules violated, 7360 violations! WTF?

Dashboard

I wish that NDepend has been a step in the CI build from day 1!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.