Higher cohesion - lower coupling

Rule ND1408 Architecture

Good architecture separates abstract namespaces (only interfaces, enumerations, delegates) from concrete ones (classes, structures). The more concrete namespaces rely only on abstract ones, the more Decoupled the architecture and the more Cohesive the classes.

This query shows, for each concrete namespace, which concrete and abstract namespaces are used.

How to fix

Transform this query into a rule if you wish to constrain your coupling / cohesion ratio.

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <Name>Higher cohesion - lower coupling</Name>
// <Id>ND1408:HigherCohesionLowerCoupling</Id>

// warnif count > 0 
let abstractNamespaces = JustMyCode.Namespaces.Where(
     n => n.ChildTypes.Where(t => !t.IsInterface && !t.IsEnumeration && !t.IsDelegate).Count() < 3 &&
          n.NbLinesOfCode <= 20
).ToHashSetEx()

let concreteNamespaces = JustMyCode.Namespaces.Except(abstractNamespaces).ToHashSetEx()

from n in concreteNamespaces
let namespacesUsed = n.NamespacesUsed.ExceptThirdParty()
let concreteNamespacesUsed = namespacesUsed.Except(abstractNamespaces)
let abstractNamespacesUsed = namespacesUsed.Except(concreteNamespaces)
orderby concreteNamespacesUsed.Count() descending
select new { 
   n, 
   concreteNamespacesUsed , 
   abstractNamespacesUsed,
   // Debt = 50.ToMinutes().ToDebt(),
   // Severity = Severity.High
}

//<Expl>
// The *{0}* is deemed as *concrete* because it contains implementations 
// and it shouldn't rely on other *concrete {1}*.
// Instead, it should depend on namespaces that contain mostly abstractions.
//</Expl>

//<Desc>
// Good architecture separates *abstract* namespaces (only interfaces,
// enumerations, delegates) from *concrete* ones (classes, structures). The
// more concrete namespaces rely *only* on abstract ones, the more
// **Decoupled** the architecture and the more **Cohesive** the classes.
//
// This query shows, for each concrete namespace, which concrete and
// abstract namespaces are used.
//</Desc>

//<HowToFix>
// Transform this query into a rule if you wish to constrain your
// *coupling / cohesion* ratio.
//</HowToFix>

Architecture: all 13 items