如何更新此类/方法以改进代码指标 [英] How to update this class/method to improve Code Metrics

查看:72
本文介绍了如何更新此类/方法以改进代码指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的初始方法

public String GetAllDocuments(string url,int pager =0)
    {
        if (SessionInfo.IsAdmin)
        {
            ReportHandler dal = new ReportHandler();
            var documents = dal.FetchDocumentsList(SessionInfo.ClientID, pager);
            string documentsDataJSON = JsonConvert.SerializeObject(documents);

            return documentsDataJSON;
        }
        else
        {
            return "Sorry!! You are not authorized to perform this action";
        }
    }

Visual Studio显示以下代码度量标准:-

Visual Studio shows the following Code Metrics:-

成员:GetAllDocuments(string,int):字符串
维护性指数:67
循环复杂度:2
类耦合:7
代码行:7

Member: GetAllDocuments(string, int) : string
Maintainability Index: 67
Cyclomatic Complexity: 2
Class Coupling: 7
Lines of Code: 7

因此,为了改进它,我对我的方法进行了如下修改:-

So in order to improve it, I modified my method as below:-

 public String GetAllDocuments(string url,int pager =0)
    {
            ReportHandler dal = new ReportHandler();
            var documents = dal.FetchDocumentsList(SessionInfo.ClientID, pager);
          //moved the JSON Conversion to Separate class
            string documentsDataJSON = JsonHandler<T>.ConvertToJSON(documents);
            return documentsDataJSON;

    }

但它仍将代码指标显示为

But still it shows the Code Metrics as

成员:GetAllDocuments(字符串,整数):字符串
维护性指数:72
循环复杂度:1
类耦合:5
代码行:5

Member: GetAllDocuments(string, int) : string
Maintainability Index: 72
Cyclomatic Complexity: 1
Class Coupling: 5
Lines of Code: 5

除非可维护性指数大于90,否则我无法提交.

I can't submit this, unless the Maintainability Index is 90+.

我还能做些什么来改善代码指标.

What else can I do to improve Code Metrics.

此外,我也在考虑创建这么小的事情,创建单独的方法/类不是开销

推荐答案

将可维护性指数作为工具而非目标.

如果您不了解为什么代码一开始就不可维护,那么增加可维护性指数毫无意义.您将一直四处移动代码以满足索引,而无需了解您在做什么.

There's no point in increasing the maintainability index if you don't realize why your code wasn't maintainable in the first place. You'll just keep moving code around to satisfy the index, without ever understanding what you're doing.

您的问题不应该

如何更新此类/方法以改进代码指标?

How to update this class/method to improve Code Metrics?

但是

如何提高此类的可维护性?

How to improve the maintainability of this class?

我现在看到了很多问题:

I see a number of problems with it right now :

  • 依赖关系是隐式的,即您是 new 直接在方法内部进行操作.这会使代码的灵活性,可组合性和可读性降低.

  • Dependencies are implicit, i.e. you're new'ing things up directly inside the method. This makes the code less flexible, composable and readable.

将ReportHandler依赖项传递到 GetAllDocuments()或类构造函数中会更好.

Passing the ReportHandler dependency in to GetAllDocuments() or the class constructor instead would be better.

可测试性很差.如果ReportHandler是接口(或抽象类),则可以在测试 GetAllDocuments()的过程中用伪造的报表处理程序替换它,以提高性能和测试.请注意,您不必使用Factory 即可,具有一个实际实现和测试实现的简单界面就足够了.

It is poorly testable. If ReportHandler were an interface (or an abstract class) instead, you could substitute it with a fake report handler in tests for GetAllDocuments() for improved performance and testing. Note that you do not have to use a Factory to do that, a simple interface with one real implementation and a test implementation are good enough.

未使用参数 url .

SessionInfo.IsAdmin 是一种魔术简写,容易出现与上述相同的问题.如果您在控制器中,那没什么大不了的,但是如果您应该在业务层中,那么这将妨碍可测试性和可维护性.

SessionInfo.IsAdmin is a kind of magic shorthand that is prone to the same range of problems as the above. If you're in a controller, it's no big deal but if you're supposed to be in a business layer, this will hamper testability and maintainability.

这篇关于如何更新此类/方法以改进代码指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆