NDepend for Sitecore Helix projects

NDepend is a tool for static code analysis. It can tell how good or bad is the code. It can be very useful to keep track of the technical debt. It can be plugged into CI\CD pipeline to check code quality continuously. It can be also very useful for Sitecore (Helix) projects.

Helix is a set of overall design principles and conventions. Here is how we can use NDepend to keep track of them:

Acyclic Dependencies Principle

The dependency graph of packages must have no cycles.

http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

NDepend already has two default rules for this: Avoid namespace dependency cycles and Avoid namespaces mutually dependent. Both rules are under Architecture group. Here are links to NDependent Rules Explorer website: rule1 and rule2.

NDepend ADP rules

To find dependency cycles you can also use NDepend Dependency Graph or Dependency Matrix views. Here is a nice video about it:

Stable Dependency Principle

The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.

http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

NDepend doesn’t have a default rule for this, but we can easily create it by ourselves:

from project in JustMyCode.Assemblies
let moreStableAssembliesUsed = project.AssembliesUsed.Where(a => a.Instability > project.Instability)
where moreStableAssembliesUsed.Any()
select new {project, moreStableAssembliesUsed}

However, for Helix projects, a more useful can be a rule that will check dependency between layers. We can write a rule like this:

from project in JustMyCode.Assemblies
where project.Name.Contains(".Foundation.") || project.Name.Contains(".Feature.")
let invalidReferences = project.AssembliesUsed.Where(a => a.Name.Contains(".Feature.") || a.Name.Contains(".Project."))
where invalidReferences.Any()
select new {project, invalidReferences}

To create a new rule, click Create Rule button then enter a query into Queries and Rules Edit window:

NDepend create a rule

NDepend QR edit

Stable Abstractions Principle

Abstractness increases with stability.

http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

NDepend has default rule for checking this. It’s called Assemblies that don’t satisfy the Abstractness/Instability principle under the Architecture group. And here is the link with the rule details: link.

NDepend SAP rule

More info

Of course, NDepend has much more features. On the website, there are walkthrough videos that explain features pretty good.