Higher cohesion - lower coupling
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.
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>
Open in the interactive Rules Explorer · All default rules
Architecture: all 13 items
- Avoid namespaces mutually dependent ND1400
- Avoid namespaces dependency cycles ND1401
- Avoid partitioning the code base through many small library Assemblies ND1402
- Enforcing Clean Architecture ND1412
- UI layer shouldn't use directly DB types ND1403
- UI layer shouldn't use directly DAL layer ND1404
- Assemblies with poor cohesion (RelationalCohesion) ND1405
- Namespaces with poor cohesion (RelationalCohesion) ND1406
- Assemblies that don't satisfy the Abstractness/Instability principle ND1407
- Avoid excessive class coupling ND1410
- Higher cohesion - lower coupling ND1408
- Avoid mutually-dependent types ND1409
- Example of custom rule to check for dependency ND9900