查找工具&修复C#编码标准

时间:2016-01-28 09:45:57

标签: c# ndepend

我使用名为NDEPEND的工具扫描我的C#解决方案以查找编码问题。该工具可以很好地根据类别列出编码违规。我的问题是,这个工具是否会对应该对列出的编码问题进行的更改提出自动建议?我找不到与此相关的任何内容。

1 个答案:

答案 0 :(得分:1)

查看default NDepend rules列表,您会看到它们都带有两个评论部分:<Description><HowToFix><HowToFix>部分包含有关如何解决问题的建议。

此功能随2015年6月发布NDepend v6而来,可能您使用的是旧版本?

例如它看起来像:

// <Name>Base class should not use derivatives</Name>

warnif count > 0 
from baseClass in JustMyCode.Types
where baseClass.IsClass && baseClass.NbChildren > 0 // <-- for optimization!
let derivedClassesUsed = baseClass.DerivedTypes.UsedBy(baseClass)
where derivedClassesUsed.Count() > 0
select new { baseClass, derivedClassesUsed }

//<Description>
// In *Object-Oriented Programming*, the **open/closed principle** states:
// *software entities (components, classes, methods, etc.) should be open 
// for extension, but closed for modification*. 
// http://en.wikipedia.org/wiki/Open/closed_principle
//
// Hence a base class should be designed properly to make it easy to derive from,
// this is *extension*. But creating a new derived class, or modifying an
// existing one, shouldn't provoke any *modification* in the base class.
// And if a base class is using some derivative classes somehow, there
// are good chances that such *modification* will be needed.
//
// Extending the base class is not anymore a simple operation,
// this is not good design.
//</Description>

//<HowToFix>
// Understand the need for using derivatives, 
// then imagine a new design, and then refactor.
//
// Typically an algorithm in the base class needs to access something 
// from derived classes. You can try to encapsulate this access behind 
// an abstract or a virtual method.
//
// If you see in the base class some conditions on *typeof(DerivedClass)*
// not only *urgent refactoring* is needed. Such condition can easily 
// be replaced through an abstract or a virtual method.
//
// Sometime you'll see a base class that creates instance of some derived classes.
// In such situation, certainly using the *factory method pattern* 
// http://en.wikipedia.org/wiki/Factory_method_pattern
// or the *abstract factory pattern* 
// http://en.wikipedia.org/wiki/Abstract_factory_pattern
// will improve the design. 
//</HowToFix>
相关问题