BT

Your opinion matters! Please fill in the InfoQ Survey!

Using NDepend and LINQ to Examine Code

| by Jonathan Allen Follow 186 Followers on Jun 12, 2012. Estimated reading time: 1 minute |

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

Adoption Stage
Style

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

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT