Avoid empty interfaces

Rule ND1212 Object Oriented Design

An empty interface defines no members, hence no contract to implement. It is probably used as a marker to identify a group of types: for run-time identification use a custom attribute instead; for compile-time identification an empty interface is acceptable.

An empty interface implementing at least one other interface is not matched: implementing it requires implementing sub-interfaces members.

How to fix

Remove the interface or add members. If used as a marker, replace it with a custom attribute.

Estimated Debt: 10 minutes to discard the interface plus 3 minutes per implementing type.

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>Avoid empty interfaces</Name>
// <Id>ND1212:AvoidEmptyInterfaces</Id>
warnif count > 0 from t in JustMyCode.Types where 
   t.IsInterface && 
   t.NbMethods == 0 &&
  !t.InterfacesImplemented.Any()
select new { 
   t, 
   t.TypesThatImplementMe,
   Debt = (10 + 3*t.TypesThatImplementMe.Count()).ToMinutes().ToDebt(),
   Severity = t.TypesThatImplementMe.Any() ? Severity.Medium : Severity.Low
}

//<Expl>The *{0}* is empty and is implemented by *{1}*. If the empty interface is being used to mark a set of classes, replace the interface with a custom attribute.</Expl>

//<Desc>
// An empty interface defines no members, hence no contract to implement. It
// is probably used as a marker to identify a group of types: for run-time
// identification use a custom attribute instead; for compile-time
// identification an empty interface is acceptable.
//
// An empty interface implementing at least one other interface is not
// matched: implementing it requires implementing sub-interfaces members.
//</Desc>

//<HowToFix>
// Remove the interface or add members. If used as a marker, replace it with
// a custom attribute.
//
// Estimated Debt: 10 minutes to discard the interface plus 3 minutes per
// implementing type.
//</HowToFix>

Object Oriented Design: all 14 items