谁能告诉我为什么这段代码的可维护性指数只有 40

本文关键字:可维护性 指数 代码 告诉我 为什么 段代码 | 更新日期: 2023-09-27 18:30:58

我不知道为什么这种方法的可维护性指数(在Visual Studio中计算)只有40,我实际上必须删除除前两行之外的几乎所有行才能超过60:

    public void getNewPasswordDetails(Int32 newPasswordId)
    {
        int UserId = HttpContext.Current.User.Identity.GetUserId().ToInt();
        bool userIsAdmin = HttpContext.Current.User.IsInRole("Administrator");
        //get a list of userIds that have UserPassword records for this password
        var UserIDList = DatabaseContext.UserPasswords.Where(up => up.PasswordId == newPasswordId).Select(up => up.Id).ToList();
        //Retrive the password -if the user has access
        Password newPassword = DatabaseContext.Passwords
                                                        .Include("Creator")
                                                        .Where(pass => !pass.Deleted
                                                        && (
                                                            (UserIDList.Contains(UserId))
                                                         || (userIsAdmin && ApplicationSettings.Default.AdminsHaveAccessToAllPasswords)
                                                         || pass.Creator_Id == UserId)
                                                            )
                                                            .Include(p => p.Parent_UserPasswords.Select(up => up.UserPasswordUser))
                                                            .SingleOrDefault(p => p.PasswordId == newPasswordId);

        if (newPassword != null)
        {
            //map new password to display view model
            AutoMapper.Mapper.CreateMap<Password, PasswordItem>();
            PasswordItem returnPasswordViewItem = AutoMapper.Mapper.Map<PasswordItem>(newPassword);
            //generate a string based view of the new category
            string passwordPartialView = RenderViewContent.RenderViewToString("Password", "_PasswordItem", returnPasswordViewItem);
            //broadcast the new password details
            PushNotifications.sendAddedPasswordDetails(passwordPartialView, returnPasswordViewItem.Parent_CategoryId, returnPasswordViewItem.PasswordId);
        }
        else
        {
            //we dont have access any more, so tell UI to remove the password
            PushNotifications.sendRemovePasswordAccess(new PasswordDelete()
                                                                { 
                                                                     PasswordId = newPasswordId
                                                                });
        }
    }

谁能告诉我为什么这段代码的可维护性指数只有 40

代码越复杂,维护就越困难。右?因此,让我们看一下所呈现的复杂代码部分。我将检查它,就好像我是第一次查看代码的开发人员一样

DatabaseContext.Passwords
               .Include("Creator")
               .Where(pass => !pass.Deleted
                          && ((UserIDList.Contains(UserId))     // Why Check #1
                               || (
                                   userIsAdmin                // Why Check #2
                                   &&                        // Why Check #3
                                   ApplicationSettings.Default.AdminsHaveAccessToAllPasswords
                                  )
                              || pass.Creator_Id == UserId) 
                             )
               .Include(p => p.Parent_UserPasswords.Select(up => up.UserPasswordUser))
               .SingleOrDefault(p => p.PasswordId == newPasswordId);

在进入循环之前,人们已经知道这些状态事实:

  1. (UserIDList.Contains(UserId)为什么检查#1
  2. userIsAdmin为什么检查#2
  3. (userIsAdmin && ApplicationSettings.Default.AdminsHaveAccessToAllPasswords)为什么检查#3

然而,开发人员已将这些检查降级为对密码中的每个密码进行。为什么?难道没有更好的表达方式吗?

复杂性的发生是由于程序逻辑的应用(编码),对于确定业务逻辑的每个逻辑分支直接增加了代码的复杂性以及随后的未来可维护性;因此获得了评级。

当然,就其本质而言,代码具有某些复杂性,这将会发生并且应该在意料之中。问题是,这种复杂性能否最小化到可以更好地实现代码维护的程度。