If we are working on a big project with many developers working on it, the architect and the technical leads of the project has to go through the painful process of reviewing the code which each developer has written checking if the developer has written the code keeping the principles of OOP in mind or has he written the code using the guidelines mentioned by the architect or the technical manager.
Some of the general guidelines which every technical manager wants to follow in his or her project can be.
- To have some naming conversion for the methods and variables in the class like following Pascal case
- The developer should not dereference the variable having null memory location.
- How easily can we extend the functionality of the classes
- What can be the effect of changing the functionality of the class for future use.
And why should we follow these good practices while coding for the projects. By following these practices and many more we can achieve many benefits, some of which I have mentioned below.
- Maintaining the code is easier.
- Extending the functionalities to adhere the future needs is easier.
- Developers learn the good practices of writing and maintaining the code.
- The trivial mistakes which can otherwise lead to bigger and costlier maintenance can be reduced.
In this article I want to discuss NDepend which is one of the tools which I have found useful which can help us to monitor all the points which I have discussed above and many more.
NDepend has been developed by Partick Smacchia, who has given me an opportunity by giving me the license of the latest version (i.e. v 6.0) of the tool to analyze and write about it on my blog.
Though the tool is very vast and it contains lot of functionalities which would be difficult for me to cover in this single article but I will try my best to analyze and write about it as much as possible.
First of all when I installed and noticed the dashboard of the tool, it took me lots of time to understand the tool and check how it can be useful for me or for anyone working on .NET technologies. Then I invested some time to understand it in better way.
The ease with which the tool can be installed shows that how the developer of this tool has made the installation a painless process.
We just need to copy all the files (along with the NDependLicence.xml licence file) provided with the installer in one of the folders on our machine and double click on the NDepend.VisualStudioExtension.Installer.exe file which will open the window which prompts the installation window for the visual studio installation. More information about the installation and getting started can be found in the Ndepend’s blog at http://www.ndepend.com/docs/getting-started-with-ndepend
After installing the Ndepend we should be able to see one new menu in the visual studio solution. Now coming to the analyzing part of the visual studio project using Ndepend. After opening the project which we want to analyze we should go to the Ndepend menu and click the Attach New Ndepend Project to the current VS Solution as shown in the figure below.
Once clicked we will get a new window showing the assemblies of the project will pop up on the screen. Here on this window we will get the all the assemblies of the project which will be analyzed as shown in the figure below.
Once we click the analyze button we will find that the project which we have selected has been analyzed by the pre-defined rules which come loaded with the tool.
Once we analyze the solution we should be able to see the dashboard of the Ndepend which comes bundled with lots of the information. This information is generated using the Project Rules which are in turn generated using the CQL which I will discuss in the next section of the article.
Querying the Code base
Yes you read it right. We can query the code base of our project using the Ndepend’s built in functionality CQL(Code Query Language). Like SQL ,Linq which is used to query the information from the database and collections classes respectively we can use CQL to get the information from the code base. CQl is very familiar to those who know SQL and also to those who know Linq.
Writing my First CQL to analyze the code base
After attaching my project to the NDepend solution, I should be able to write my first CQL to analyze my code base, for which I need to open the Queries And Rule Edit window which we can find under NDepend -> Rule -> New -> ..Code Query
I wanted to know in my project the number of methods which are public and abstract, for that I have executed the following query
Methods.Where(m => m.IsPublic && m.IsAbstract)
Which in turn return me the following result for the project I am analyzing.
Checking the Rules
Rules are industry standard set of practices which we should follow to develop a great product. NDepend comes preloaded with some predefined set of rules.
We can also create a set of rules by using the “warnif” keyword along with the CQL which we have learned to develop in my previous part of this article.
Suppose I want to check and run a rule to check the functions which have more than four lines of code in my project. I will open the CQL creator and write the following code
// <Name>Number Of Lines</Name>
warnif count > 0
Methods.Where(item => item.NbLinesOfCode > 5)
And if we open the Queries and Rule Explorer we can find the newly created there itself
Code Metrics View
Ndepend comes with the code metrics view to analyze the code visually. With metrics view we can get to know the intricacies of the code like number of lines in the type or number of types or the methods which are without any comments.
One of the metric which I want to show as an example is the depth of inheritance. The figure below shows the depth of inheritance metric for my sample project
As depth of inheritance can be found only for the type, that is why we need to Select Level as Type and Size as Depth Of Inheritance in the code metric window as shown in the figure above.
This window will show number of boxes, on the hovering over these boxes we can get to know the level of inheritance . As in my case the level of inheritance for my VintageCar class in Car namespace is 1.
One more example I want to discuss here is IL Nesting depth which gives us the idea of the nesting levels in the code for the methods being analyzed. This metric will show the number of cases and conditions inside the methods for the assembly. Generally it is considered good the to split a method with more cases and conditions in multiple smaller methods.
Since my sample project is very small and it doesn’t have IL nesting depth more than 1.
There are many types of already created principles present in the NDepend which can be very helpful for attaining the perfect OOP principles in the project.
Though the Ndepend tool is very useful to analyze out code base but it is equally time consuming to understand it and fully utilize all its functionalities. But I can assure that once you are very comfortable with the tool it can be very useful for the your project and team of developers .
In this article I have covered the CQL, code rules and code metric one by one to be utilized in the project.
The Ndepend’s documentation is quite enough to get the knowledge of the tool.
You can download the trail version of the tool at http://www.ndepend.com/download