A stateless class or structure might be turned into a static type
Matches non-static, non-generic classes/structures with no instance fields, no interface implemented and no base class (other than System.Object): a stateless collection of pure functions, better hosted in a static class so clients needn't create an object.
How to fix
Declare all methods static and make the class/structure static. Severity is Low: more an advice than a problem.
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>A stateless class or structure might be turned into a static type</Name>
// <Id>ND1206:AStatelessClassOrStructureMightBeTurnedIntoAStaticType</Id>
warnif count > 0
from t in JustMyCode.Types where
!t.IsStatic &&
!t.IsGeneric &&
!t.IsAbstract &&
t.InstanceFields.Count() == 0 &&
t.SimpleName != "Program" && // Don't warn on Program classes generated by designers
// Don't match:
// --> types that implement some interfaces.
t.NbInterfacesImplemented == 0 &&
// --> or classes that have sub-classes children.
t.NbChildren == 0 &&
// --> or classes that have a base class
((t.IsClass && t.DepthOfDeriveFrom("System.Object".AllowNoMatch()) == 1) ||
t.IsStructure) &&
// Don't warn about classes instantiated by Dependency Injection frameworks
!t.TypesUsingMe.Any(t1 => t1.TypesUsed.ParentNamespaces().Any(n => n.Name.StartsWithAny(
"Microsoft.Extensions.DependencyInjection",
"Autofac", "Microsoft.Practices.Unity", "Ninject",
"StructureMap", "SimpleInjector", "Castle.Windsor",
"LightInject", "Spring", "Lamar", "AutoMapper"
))) &&
!t.BaseClasses.Any(bc => bc.FullName.StartsWith("AutoMapper")) &&
// Don't match marker classes
!(t.IsSealed && t.Members.Count() == 1 &&
t.Members.Single().IsMethod &&
t.Members.Single().AsMethod.IsConstructor &&
t.Members.Single().AsMethod.NbParameters == 0)
let methodsUsingMe = t.TypesUsingMe.ChildMethods().Where(m => m.IsUsing(t))
select new {
t,
methodsUsingMe,
Debt = (1 + methodsUsingMe.Count()).ToMinutes().ToDebt(),
Severity = Severity.Low
}
//<Expl>
// The *{0}* doesn't contain any instance field.
// Since it solely performs processing without managing any state,
// should it be refactored into a *static class*?
//</Expl>
//<Desc>
// Matches non-static, non-generic classes/structures with no instance
// fields, no interface implemented and no base class (other than
// *System.Object*): a *stateless* collection of *pure* functions, better
// hosted in a **static class** so clients needn't create an object.
//</Desc>
//<HowToFix>
// Declare all methods *static* and make the class/structure *static*.
// Severity is **Low**: more an advice than a problem.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
Object Oriented Design: all 14 items
- Avoid interfaces too big ND1200
- Base class should not use derivatives ND1201
- Class shouldn't be too deep in inheritance tree ND1202
- Class with no descendant should be sealed if possible ND1203
- Overrides of Method() should call base.Method() ND1204
- Do not hide base class methods ND1205
- A stateless class or structure might be turned into a static type ND1206
- Non-static classes should be instantiated or turned to static ND1207
- Methods should be declared static if possible ND1208
- Constructor should not call a virtual method ND1209
- Avoid the Singleton pattern ND1210
- Don't assign static fields from instance methods ND1211
- Avoid empty interfaces ND1212
- Avoid types initialization cycles ND1213