KEEP CONTROL OF YOUR CODE QUALITY
Motivation
I am working as the CTO at the Fairmas GmbH. We develop financial planning tools for the hotel industry, especially to support budgeting and forecasting processes including management reporting. We develop desktop applications as well as web applications. Another great tool from Fairmas is the Benchmarking system, where hotels can compare their business figures on a daily base.
For every software project in my company, we come to the point, where we have to define the quality management process. Of course, this process is based on our company standards, but for every project we have to define the specific quality metrics that are important. These metrics should then be measured all over the development process. Measurement and evaluation Tasks have to be integrated. Some metrics are measured more often than others. Measurement results must be reviewed during a development sprint, other at the end of a sprint and so on.
So we need a tool that supports a wide range of quality metrics and that provides reporting features for live evaluation and consolidated report generation. Integration of the tool must be easy. We came very fast to the decision to use NDepend for our purposes. In this blog post, I like to write down some of our experiences with this tool. I want to show how easy it is to control your code quality!
What is NDepend?
NDepend is a tool for static code analysis of .NET projects and assemblies. NDepend analyzes and reports a wide range of code metrics (e.g. lines of code, number of types, comment coverage, depth of inheritance, third-party usage, method complexity, code coverage by tests and so on). It shows results in numerical form and visualizes metrics and architecture in a big bunch of diagrams. NDepend can be used as a standalone tool as well as direct integration into Visual Studio. Reports/Results are shown in numerical form and diagrams directly in the standalone tool or in Visual Studio for live evaluation. Further, reports are generated as HTML pages and can so be used for reporting to the QM and development teams on a regularly base. You can configure when NDepend analyzes your code and generates reports (e.g. after every build or on a regular time interval).
NDepend can be purchased and downloaded here: http://www.ndepend.com/.
Due to my close contact to the team, I can tell something about the project and the team itself. The NDepend team starts development of the tool at 26th April 2004 as a small, 2-day OSS project to test code metrics proposed by Robert C. Martin (http://en.wikipedia.org/wiki/Robert_Cecil_Martin) for .NET projects. After the team noticed that there is a need for such a tool (only by counting downloads), it took until 2007 to come to a professional edition. Since this time, NDepend is under continuous development. New versions are released regularly. Find the release note here: http://www.ndepend.com/release-notes. The team is driven by passion for work accomplished and work to be done. The team wants to provide a comprehensive view on your code metrics in an optimal way. Therefore, they started the user voice where user can give feedback and provide feature suggestions: http://ndepend.uservoice.com/
Let’s have a closer look at the tool.
Installation
Installation is very easy. The reason: it is not needed. You can start NDepend directly. In the standalone tool you can starts analysis by simply selecting .NET projects or assemblies. You have access to all NDepend features within this tool. Further, the standalone tool provides a functionality to install the Visual Studio addin that makes all the NDepend functionality available directly in VS. There is also an addin for the .NET Reflector. The following image shows this standalone tool. Everything is quite easy. But in my opinion, an installer package that installs the standalone tool and the appropriate addins for Visual Studio would improve the experience.
Visual Studio Integration
NDepend integrates in Visual Studio via the main menu where you can access all the features provided. There are also context-sensitive features that are available via the context menu per project in the solution. To use NDepend in your solutions, you have to attach an NDepend project to it. Analysis information will be stored within this project that are used to show all the information about the code to you. Add an NDepend project to your solution via the main menu of Visual Studio. The following image shows this integration.
Choose “Attach new NDepend Project to current VS Solution” from the main menu and add or remove projects to want to analyze in the popup dialog that appears. Start the analyze process by clicking he button “Analyze x .NET Assemblies”. When the checkbox “Build Report” is checked, a browser window with the first report appears after analysis finished. I will show this report later in this post.
NDepend has analyzed your current code base now. You can now browse your code metrics with NDepend. A first point to start is the dashboard. You can open the dashboard via the NDepend\Dashboard main menu in Visual Studio. You should see something that is similar to the following image.
The dashboard gives you a first impression of all the metrics measured in your projects. It shows some metrics in numerical form, others are visualized in diagrams. The dashboard is very dynamic, so you can zoom in and out in diagrams, edit the diagrams, export charts and dive into the details, as the next images shows.
Live Analysis
NDepend provides a rich set of visualizations for code metrics that can be used whenever you are in the code. Via the Visual Studio main menu and the context menu of the projects in the solution, you can show e.g. the dependency matrix, the dependency graph or the metrics view. In this views you can explore dependencies as well as complexity of your code. The following image shows these graphs.
Within the graphs you can highlight the various elements. The views are synchronized, so that highlighted elements are highlighted in all open views. So you can find the appropriate element very fast from view to view. With double-click on those items, NDepend opens the corresponding code in Visual Studio. Double-clicks on components opens a list of the contained files from where you can jump into the code. That makes exploring the code and navigating between all the views very easy. A tooltip is always there and explains the items you are currently highlighting. In my opinion, that’s a great feature. It is very user-friendly because I am guided through all the complex views by NDepend and I can find code with critical problems very fast. That is, what NDepend makes a premium tool from my point of view.
With these tools, the development team can evaluate the quality of their code live and in “real-time”. That gives your team the power to adapt and improve the code during a sprint. Quality metrics are made visible. This reduces development costs, because problems can be addressed and eliminated very early – just in the same sprint when the code is written. You do not have to wait until the QS team returns with results from code metric measurements.
Analysis over Time
As described above, NDepend provides tools that gives you a live view on your code and the measured metrics. But that is not all. NDepend collects data over time to generate trend reports. These trend reports show how the quality of code changes. From this you can derive code quality goals for upcoming sprints and monitor the results. Trend reports can be viewed at the dashboard. There you can create new trend reports that are essential to your selected metrics and quality standards. These trend reports/charts will be part of the dashboard. The following image shows the dashboard with the default trend reports/charts and some data I collected for this blog post.
Extensibility
NDepend is very flexible and extensible regarding the code metrics. The development team implemented an absolutely cool feature to reach this goal: CQLinq (http://www.ndepend.com/docs/cqlinq-syntax). CQLinq stands for Code Query Linq, depends on the Linq language feature in .NET and lets you query the code very easily. Code metrics in NDepend are defined as rules and queries based on CQLinq. Because CQLinq has a rich set of statements to query your code, you can express complex queries with this language. NDepend provides the “Queries and Rules Explorer” and an edit dialog for editing these queries. The next image shows both views. You can open these views via the Visual Studio main menus NDepend\Rule\View Edition Panel and NDepend\Rule\View Explorer Panel.
You can add new queries and rules very easily. NDepend then checks this rule in the analyzing process. You can also add these queries as trend charts to your dashboard. This feature provides a very flexible and powerful tool to adapt NDepend to your needs. You can find a list and documentation of all NDepend metrics here: http://www.ndepend.com/docs/code-metrics.
Reporting
As mentioned before, NDepend generates an HTML-based report about the code quality metrics. The next image shows this report. The report provides a short overview of the metrics as well as a summary of the violated rules. You can drill down into the details by following the links in the report. The report is well-structured and shows everything the QS team needs for further investigations. Because the report is HTML-based, you can share it with everyone who needs the information.
Build Process
NDepend is easy to integrate into a build process. You can find help and guidelines for some popular integration servers on the NDepend web site (e.g. TFS, TeamCity, FinalBuilder, CruiseControl.NET). Have a look at TFS integration here: http://www.ndepend.com/docs/tfs-integration-ndepend.
Once integrated into your build process, NDepend delivers reporting about your code metrics with every integration build. You can send the report described above to the development team, to the QS team and, of course, to all other stakeholders.
How we use NDepend
We at Fairmas integrated NDepend all over our development processes. At the beginning of a project, we define the important code metrics we want to measure. Then we configure NDepend for our needs, integrate it into the build process and give it to our developers. So they can analyze and monitor the quality of their code with the live tools integrated into Visual Studio. Reports are generated regularly by our TFS build processes and then they are sent to the QS team as well as to the development team.
We review the results from sprint to sprint. As far as needed, we recap our defined metrics and the critical values for them in order to redefine them. Sometimes we see, that some metrics are not this important to the project, others in turn are more important. So we change the measured metrics.
Code review meetings are part of our DOD (definition of done). During this meetings, the development team reviews and discusses implementations of current user stories. We take the results from NDepend into these meetings and discuss them as well. Based on this, we define new quality goals or necessary refactorings that have to be done in the next sprint. We use trend reports to monitor if our quality goals will be met.
Fazit
NDepend is a very powerful tool. With NDepend it is an ease to monitor code quality all over the development process. The performance of the tool integration in Visual Studio is pretty good. There is a ton of information that has to be analyzed by NDepend, but operational speed of Visual Studio or the build process is not really affected. Using the live tool is fun and helps to keep control of our code quality.
CQLinq provides powerful functionality to query the code and to define own metrics and rules. I am excited to see, what the NDepend team will do with the power of the new .NET Compiler Platform (Roslyn).
As mentioned at the beginning of this post, there is also a standalone tool of NDepend that provides the same functionality as the Visual Studio integration does. So you can use the tool without using a Visual Studio license for it.
For future releases we like to see some more integration with version control. There could be reports about the developers and how they relate to violations and problems with the code quality. That would make it simpler to plan reviews and trainings. And as stated above, we like to see an installer package.
NDepend provides a lot of metrics and rules. But please be aware: you have to define the critical values per metric in your project for your own! It is on you to define which complexities, depth of inheritance, code coverage by tests and so on makes maintainability of code more difficult to you. Defining those values depends a little bit on experience. But NDepend gives a good starting point.
Start controlling your code quality and integrate NDepend into your development and build process!
Reference: | KEEP CONTROL OF YOUR CODE QUALITY from our NCG partner Robin Sedlaczek at the Robin Sedlaczek’s Blog blog. |