0

我不明白为什么这种方法的可维护性指数(在 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
                                                                });
        }

    }
4

1 回答 1

2

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

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

然而,开发人员已将这些检查委托给 Passwords 中的每个密码进行。为什么?没有更好的表达方式吗?

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

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

于 2014-11-19T19:18:07.137 回答