【问题标题】:Tools to Find & Fix C# coding standards查找和修复 C# 编码标准的工具
【发布时间】:2016-01-28 09:45:56
【问题描述】:

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

【问题讨论】:

    标签: c# ndepend


    【解决方案1】:

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

    此功能随NDepend v6 于 2015 年 6 月发布,也许您使用的是旧版本?

    例如它看起来像:

    // <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>
    

    【讨论】:

    • 谢谢帕特里克。是的,我也可以在我的 NDEPEND 版本中找到它。实际上,我认为 NDEPEND 会像在 Resharper 中一样提供建议/提示。
    猜你喜欢
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多