NDepend customize rule: Avoid defining multiple types in a source file
NDepend is a tool that offers a wide range of features to let developers analyze a .NET code base. It comes with about 200 built in rules. But there are a few default rules that do not fit “my rules”. This blog post shows how to customize one and even more important why.
Avoid defining multiple types in a source file
I completely agree, with one exception: When having a generic and a non-generic type e.g. MyClass and MyClass
warnif count > 0
let lookup =
Application.Types.Where(
t =>
t.SourceFileDeclAvailable &&
!t.IsGeneratedByCompiler &&
!t.IsNested
).ToMultiKeyLookup(
t =>
t.SourceDecls.Select(d => d.SourceFile)
)
from @group in
lookup.Where(
g =>
g.Select(
x =>
x.Name.IndexOf("<") > -1
? x.Name.Substring(0, x.Name.IndexOf("<"))
: x.Name
).Distinct().Count() > 1
)
let sourceFile = @group.Key
let typeWithSourceFileName =
@group.FirstOrDefault(
t =>
t.SimpleName == sourceFile.FileNameWithoutExtension
)
let typeIndex = typeWithSourceFileName ?? @group.First()
select new {
typeIndex,
types = @group as IEnumerable<IType>,
typeNames =
String.Join(
", ",
((IEnumerable<IType>)@group).
Select(t1=>t1.Name).
ToArray()
),
sourceFile.FilePathString
}