Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Code Analyzing and NDepend

5.00/5 (1 vote)
20 Aug 2012CPOL5 min read 9.8K  
Code analyzing and NDepend

In my childhood, my uncle showed me how to see the cloud in a close look and I understand that one can draw some elements of the Earth in the sky-canvas if he wants to. After that, the cloud comes closer to me and it teaches me one thing that, a deeper-look to something will give you some clues to draw your imagination. You will be able to see that one which you have built-up in your mind.

Years later, after completing my graduation, I started my career as a software engineer but I noticed that I do not have that much passion in my coding and development which I should be to enjoy my profession and I have started asking myself - am I doing any engineering here? Is my code becoming that thing which I have designed in my mind? So to find that answer, I tried that old solution here. I decided to come closer to my code start analyzing them. And you know what, it is really working for me and at least it gives me the confidence that I can build something that I really want to. I can draw my thinking there through my code and can build-up my vision that I have designed in my mind. Yes, Code analyzing is an amazing thing. It helps you see your code quality, matrix, design, dependency, naming conversion, purity, visibility, architecture, layering and even the dead code in your application.

I started my first code analyzing tool for DOTNET with FXCOP which I was introduced to while I reading a very nice book – Framework Design Guideline. After that, I am hoping to get closer and have freedom to go over my code analyzing. I got NDEPEND from Patrick Smacchia who has given me the opportunity to play with NDepend 4. Here, I am picking the NDEPEND to introduce a tool for analyzing your code.

I found some amazing things here in Ndepend:

Why Analyzing Code Structure, Design, Dependencies?

It helps you:

  • To avoid dependencies cycles between your components
  • To know about layering and dependencies issues in your code base
  • To prevent design erosion of your code base
  • Care about fabricated complexity and how to reduce it effectively
  • Details the Level metric definition and usage
  • Through hints on how to componentize existing code
  • To know dependencies and concerns
  • To detect all Paths from A to B
  • To re-factor, re-structure and the cost of Levelizing
  • Evolutionary Design and Acyclic componentization
  • Understanding Code: Static vs Dynamic Dependencies

Here is the dependency result for one of my applications:

all

Why Build Comparison?

It will allow you:

  • to write rules that detect API breaking changes
  • to focus code review on code that has been changed and added since the last release
  • to Quality review on code that has been changed and added since a certain milestone
  • to detect when new or refactored code is poorly covered by tests

Code Metrics

Ok, now let's talk about the Code Metrics. In Ndepend, I have found Code Metrics on:

  • Application
  • Assemblies (by measuring coupling between types of your application)
  • Namespaces
  • Type
  • Method
  • Field

clip_image004

clip_image006

Metrics on Application are going to give you the following:

  • Lines of Code (NDepend computes this metric directly from the information provided in PDB files. The LOC for a method is equal to the number of sequence points found for this method in the PDB file. A sequence point is used to mark a spot in the IL code that corresponds to a specific location in the original source.)
  • Lines of Comments (Ndepend needs PDB files present and its corresponding source files)
  • Comment Percentage
  • Lines of code covered
  • Lines of code not covered
  • And all five things mentioned above (Assemblies, Namespaces, Type, Methods and Fields)

Metrics on assemblies, Namespaces, Type, Methods and Fields (only Afferent Coupling) allow you find two main coupling here on their respective levels:

  • Afferent Coupling
  • Efferent coupling

Other things of Metrics on assemblies:

  • Relational Cohesion
  • Instability
  • Abstractness
  • Distance from main sequence

Other things of Metrics on Namespaces:

  • Level (defined for assemblies, namespaces, types, methods)

Other things of Metrics on Type –- Lack of Cohesion Of Methods

  • Cyclomatic Complexity (defined for types, methods)
  • IL Cyclomatic Complexity
  • Size of instance (defined for instance fields and types)
  • Interfaces Implemented
  • Number of Children
  • Depth of Inheritance Tree

Other things of Metrics on Methods:

  • IL Nesting Depth
  • Parameters
  • Variables
  • Overloads
  • Percentage Branch Coverage

Here is the Matrices result on one of current applications:

clip_image008

What More?

Yes, it has lot more things like:

Naming conversion, Purity, Visibility, Architecture, Layering and even the dead code in your application.

Take into this snap shot:

clip_image010

And also things like:

  • Warnings on Build Process Health
  • Harness Test Coverage Data
  • API and Power tools

But the most exciting features that I like here are Code Rule and Code Query over LINQ. It's amazing!!!!! Also, it has the intelligence support.

clip_image012

Here are some examples that I have tried to my application:

Here in the first example, I have tried to look into the type, found number of methods and field declared there.

clip_image013

And another example to search all fields which start with a certain string – say Jericho and also export the result into HTML:

Query Result

It seems pretty cool to me that it allows me to deep drive into my application.

Hope it will help you to get an introduction to Ndepend and code analyzing. Thanks guys for reading :).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)