簡體   English   中英

誰能告訴我為什么此代碼的可維護性指數僅為40?

[英]Can anyone tell me why the maintainability index is only 40 for this code?

我不知道為什么這種方法的可維護性指數(在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
                                                                });
        }

    }

代碼越復雜,維護起來就越困難。 對? 因此,讓我們看一下所介紹的代碼的復雜部分。 我將檢查它,就像我是第一次查看代碼的開發人員一樣

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的原因

但是,開發人員已將這些檢查降級為對“密碼”中的每個密碼進行檢查。 為什么? 有沒有更好的表達方式?

復雜性是由於程序邏輯的應用(編碼)而產生的,對於確定業務邏輯的每個邏輯分支 直接增加了代碼的復雜性,進而增加了未來的可維護性。 因此獲得了評分。

當然,就其本質而言,人們在代碼中會具有某些復雜性,這將要發生並且應該被期待。 問題是,能否將復雜性降低到可以更好地實現代碼維護的程度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM