Click here to Skip to main content
15,867,686 members
Articles / All Topics

Code Analyzing and NDepend

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
20 Aug 2012CPOL5 min read 9.3K   2   2
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)


Written By
Team Leader PracticePRO Software Systems Inc
United States United States
In my childhood, my uncle has shown 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/she wants to. After that the cloud becomes 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 can able to see that one which you have build-up in your mind.

Years past, I have started my career as a software engineer and has been looking for passion in my coding and development which I should be to enjoy my profession and has 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 have tried that old solution here... I have decided to come closer to my code and start analyzing them. And 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. It also helps me to think out of the box, solve each problems by making blocks and make me careful on each steps.

• Morshed's Technical Blog site: http://morshedanwar.wordpress.com/

• Morshed's Technical articles those are published in Codeproject site: http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=2992452

• Morshed's Linkedin profile: http://www.linkedin.com/in/morshedanwar

• Morshed's Facebook Profile : http://www.facebook.com/morshed.pulok

Beside all these I like to do - photography and music. Here is my Flickr photos : http://www.flickr.com/photos/morshed_anwar/

Comments and Discussions

 
QuestionLine and Text Formatting Required Pin
_Amy19-Aug-12 20:20
professional_Amy19-Aug-12 20:20 
AnswerRe: Line and Text Formatting Required Pin
Morshed Anwar20-Aug-12 0:59
professionalMorshed Anwar20-Aug-12 0:59 
Thanks Amit to inform. I didn't noticed that my article has automatically fetched 2 days ago. Here I just have done some text formatting here.
Md. Morshed Anwar | Senior Software Engineer
Adaptive Enterprise Limited
Blog: http://morshedanwar.wordpress.com/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.