page_title: NDepend
section_title: Code rules through LINQ queries (CQLinq)
hierarchical_section_title: NDepend / Code rules through LINQ queries (CQLinq)
caption_attribution_description: English: CQLinq Editor Overview
context_page_description: NDepend is a static analysis tool for .NET managed code. The tool supports a large number of code metrics that allow one to visualize dependencies using directed graphs and dependency matrix. The tool also performs code base snapshot comparisons, and validation of architectural and quality rules. User-defined rules can be written using LINQ queries. This feature is named CQLinq. The tool also comes with a large number of predefined CQLinq code rules. Code rules can be checked automatically in Visual Studio or during continuous integration.
context_section_description: All recent versions of the tool (after V4) provides live code queries and code rules through LINQ queries. This is one of the main innovations of NDepend. For example:
  - Base class should not use derivatives:
  // <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 }
  - Avoid making complex methods even more complex (source code cyclomatic complexity):
  // <Name>Avoid making complex methods even more complex (source code cyclomatic complexity)</Name>
  warnif count > 0 
  from m in JustMyCode.Methods where
   !m.IsAbstract &&
    m.IsPresentInBothBuilds() &&
    m.CodeWasChanged()
  let oldCC = m.OlderVersion().CyclomaticComplexity
  where oldCC > 6 && m.CyclomaticComplexity > oldCC 
  select new { m,
      oldCC, 
      newCC = m.CyclomaticComplexity, 
      oldLoc = m.OlderVersion().NbLinesOfCode,
      newLoc = m.NbLinesOfCode,
  }
  Additionally, the tool provides a live CQLinq query editor with code completion and embedded documentation.
caption: CQLinq editor and query result displayed
exif: {}