当前位置: 首页 > 知识库问答 >
问题:

重构这种方法,将其认知复杂度从21降低到允许的15。如何重构和降低复杂度

赖星驰
2023-03-14

如何降低给定代码段的复杂性?我在Sonarqube中得到了这个错误-->重构这个方法,将其认知复杂度从21降低到允许的15。

this.deviceDetails = this.data && {...this.data.deviceInfo} || {};
    if (this.data && this.data.deviceInfo) {
      this.getSessionInfo();
      // tslint:disable-next-line: no-shadowed-variable
      const { device, driver, ipAddress, port, active, connectionType } = this.data.deviceInfo;
      this.deviceDetails = {
        name: device.name || '',
        manufacturer: device.manufacturer || '',
        deviceType: device.deviceType || '',
        model: device.model || '',
        description: device.description || '',
        managerId: device.deviceManager && device.deviceManager.managerId || null,
        locationId: device.location && device.location.locationId || null,
        active: device.active,
        connectionType: connectionType || null,
        driver_id: driver && driver.driverId || null,
        ipAddress: ipAddress || '',
        port: String(port) || '',
        connectionStatus: active,
      };
      this.oldDeviceDetails = {...this.deviceDetails};
      this.deviceLocation = device.location && device.location.locationId || null;
    } else {

共有1个答案

微生俊
2023-03-14

所有这些加在一起,看起来像是一个糟糕的练习。您可以将this.devicedetails={...转换为自己的映射函数,以快速解决方案。

 类似资料: