Using NDepend and LINQ to Examine Code

by Jonathan Allen on Jun 12, 2012 |

NDepend has released a new version of their flagship static code analysis product. NDepend 4 introductes Code Query LINQ, NDepend.API, and VS 2012 support. Code Query LINQ is just what it sounds like, a LINQ provider for writing queries against code. Here is a quick example from Patrick Smacchia’s production announcement.

// <Name>Base class should not use derivatives</Name>
warnif count > 0
from baseClass in JustMyCode.Types
where baseClass.IsClass && baseClass.NbChildren > 0 // <-- for optimization!
let derivedClassesUsed = baseClass.DerivedTypes.UsedBy(baseClass)
where derivedClassesUsed.Count() > 0
select new { baseClass, derivedClassesUsed }

The source, JustMyCode, is used to differentiate between user-generated and code-generated types. Flowing down from that is a query to see if any classes are referencing their own sub-classes by name.

NDepend has the ability to look at many facets. In one of his examples, Patrick shows using it to identify high risk code by comparing code coverage to cyclomatic complexity. This technique is based on Alberto Savoia’s Change Risk Analyzer and Predictor research project.

You’ve probably noticed the warnif prefix in the above example. This is used by NDepend to create static analysis rules for their standard tools. If you want to build your own static analysis tools, then you can leverage the newly released NDepend.API. The feature list includes the ability to:

  • Analyze one or several .NET assemblies and create NDepend reports (only with Build Machine licensing)
  • Explore dependencies between this assemblies namespaces, types, methods and fields
  • Gather usual code metrics, computed by NDepend on code elements, and create your own code metrics
  • Explore diff between two versions analyzed of a .NET code base, and even follow evolution across many versions
  • Open source file declaration(s) of a code element
  • Generate on the fly and execute CQLinq rules or queries

Patrick's article titled An Original Algorithm to Find .NET Code Duplicate shows the NDepend.API in action. 

NDepend is a commercial product but they do make some affordances for the open source community. Patrick writes,

However our commitment to develop a durable win-win relation with the community hasn't changed however. To all students/teachers/OSS developers/bloggers/MVPs... we are glad to offer free pro license, and hopefully they can gently help us spread the word.

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

General Feedback
Marketing and all content copyright © 2006-2016 C4Media Inc. hosted at Contegix, the best ISP we've ever worked with.
Privacy policy

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.