【问题标题】:How to optimize my if else code c#如何优化我的 if else 代码 c#
【发布时间】:2015-07-08 07:21:17
【问题描述】:

我有以下代码:

TryUpdateModel(model);
            if (ModelState.IsValid && model.FullNameIsChecked == true && model.LookingFor == null)
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
                model.ContactList.Sort((x, y) => string.Compare(x.FullName, y.FullName));
            }
            else if (ModelState.IsValid && model.FullNameIsChecked == false && model.LookingFor == null)
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
                model.ContactList.Sort((x, y) => string.Compare(x.Email, y.Email));
            }
            else if (ModelState.IsValid && model.LookingFor != null && model.FullNameIsChecked == true)
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
                model.ContactList = contactRepository.GetAll(filter: x => x.FullName.Contains(model.LookingFor));
                model.ContactList.OrderBy(f => f.FullName);
            }
            else if (ModelState.IsValid && model.LookingFor != null && model.FullNameIsChecked == false)
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
                model.ContactList = contactRepository.GetAll(filter: x => x.Email.Contains(model.LookingFor));
                model.ContactList.OrderBy(e => e.Email);
            }
            else
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
            }

我正在使用带有 Razor 引擎的 .NET MVC 5,并使用上面的代码进行排序操作。我可以优化我的 If else 部分代码吗?我唯一想到的是使用 switch - case operator 但它看起来几乎相似。 提前致谢

【问题讨论】:

    标签: c# .net model-view-controller


    【解决方案1】:

    你所有的案例都适合这个:

    //Place this here since it's non conditional
    model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
    
    
    if(ModelState.IsValid)
    {
        if(model.LookingFor == null)
        {
            if(model.FullNameIsChecked){
                //...
            }else
            {
                //...
            }
        }else
        {
            if(model.FullNameIsChecked){
                //...
            }else
            {
                //...
            }
        }
    }
    else
    {
        //... Actually you don't need this case...
    }
    

    【讨论】:

    • 你并没有表明某些代码是无条件的。
    • 感谢您的帮助。问题是我的导师说在 if 运算符中多次使用 If 运算符是不好的做法......换句话说,他告诉我不要这样做:If () { if (){ if() { 等等。 .} } }
    • @StoyanPetkov 但是扩展嵌套条件更糟糕。
    【解决方案2】:
    • 我在你的代码检查状态中看不到 ModelState 的任何用途
    • if条件下不需要比较bool, 即

      if(true)
      

      相同
      if(bool variable=true)
      
    • 使用String.IsNullOrEmpty检查null

       TryUpdateModel(model);
      
        model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
      
              if(  String.IsNullOrEmpty(model.LookingFor))
              {
                  if(model.FullNameIsChecked)
                  {
                      model.ContactList.Sort((x, y) => string.Compare(x.FullName, y.FullName));
                  }
                  else
                  {
                      model.ContactList.Sort((x, y) => string.Compare(x.Email, y.Email));
                  }
              }
              else
              {
                  if(model.FullNameIsChecked)
                  {
                     model.ContactList = contactRepository.GetAll(filter: x => x.FullName.Contains(model.LookingFor));
                     model.ContactList.OrderBy(f => f.FullName);
                  }
                  else
                  {
                     model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
                     model.ContactList = contactRepository.GetAll(filter: x => x.Email.Contains(model.LookingFor));
                  }
              }
      

    【讨论】:

    • 这就是我要找的。 If 运算符中最多有一个 If 运算符。很好的答案。谢谢!
    • @StoyanPetkov 但它不必要地调用GetAll 两次,这可不好。
    • @Jodrell 是的,刚刚注意到,我没有进入他的业务逻辑,但肯定需要查看
    【解决方案3】:

    model.LookingFor 不为空时,您调用contactRepository.GetAll() 两次。第一次打给contactRepository.GetAll(filter: x => x.UserId == user.Id) 是多余的,对吧?

    有时您拨打.Sort(),有时您拨打.OrderBy()。为什么不一致?

    您已将xfe 用作虚拟变量。我建议始终使用一些虚拟变量——也许c 用于“联系人”。

    无论如何,这段代码想要做某种GetAll(),然后可能是排序。所以,让我们写一个对GetAll() 的调用和一个对OrderBy() 的调用。

    TryUpdateModel(model);
    model.ContactList = contactRepository.GetAll(filter:
        !ModelState.IsValid ?      (c => c.UserId == user.Id) :
        model.LookingFor == null ? (c => c.UserId == user.Id) :
        model.FullNameIsChecked ?  (c => c.FullName.Contains(model.LookingFor)) :
                                   (c => c.Email.Contains(model.LookingFor))
    );
    if (ModelState.IsValid)
    {
        model.ContactList.OrderBy(model.FullNameIsChecked ?
            (c => c.FullName) : (c => c.Email)
        );
    }
    

    【讨论】:

    • contactRepository.GetAll(filter: x => x.UserId == user.Id),是多余的吧?不,这是必要的。我有一个包含联系人列表的用户。因此,当我第一次调用contact.GetAll() 时,我调用它是因为我需要获取当前用户的联系人。如果用户想要对他的联系人进行排序 Asc。我第二次调用 .GetAll() 以便它们将按顺序显示。我真的很喜欢你的进步。我很感激。谢谢大佬。
    【解决方案4】:
    TryUpdateModel(model);
    if(ModelState.IsValid)
    {
      model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
      if(model.LookingFor == null)
      {
            if(model.FullNameIsChecked)
            {
                 model.ContactList.Sort((x, y) => string.Compare(x.FullName, y.FullName));
            }
            else
            {
                 model.ContactList.Sort((x, y) => string.Compare(x.Email, y.Email));
            }
      }
      else
      {
            if(model.FullNameIsChecked)
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.FullName.Contains(model.LookingFor));
                model.ContactList.OrderBy(f => f.FullName);
            }
            else
            {
                model.ContactList = contactRepository.GetAll(filter: x => x.Email.Contains(model.LookingFor));
                model.ContactList.OrderBy(e => e.Email);
            }
      }
    }
    

    【讨论】:

    • 不需要,因为模型状态无效。
    • 感谢您的帮助。问题是我的导师说在 if 运算符中多次使用 If 运算符是不好的做法......换句话说,他告诉我不要这样做:If () { if (){ if() { 等等。 .} } }
    • 但是在你的问题中,如果 ModelState.Isvalid = true,model.FullNameIsChecked = false,model.LookingFor != null,If 条件将被检查大约 12 次。但是,使用这个解决方案,它会减少到3。在你的问题中,如果model.IsValid = false,它将经过所有的if,else if检查,并且条件将被检查12次以上,最后它会到达其他地方。优化不是格式,而是性能调整。
    【解决方案5】:

    erm,这样每个条件都只检查一次。

    if (ModelState.IsValid)
    {
        if (string.IsEmptyOrWhitespace(model.LookingFor))
        {
            model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
        }
        else
        {
            model.ContactList = contactRepository.GetAll(
                    filter: x => x.FullName.Contains(model.LookingFor));
        }
    
        if(model.FullNameIsChecked)
        {
            model.ContactList.OrderBy(f => f.FullName);
        }
        else
        {
            model.ContactList.OrderBy(e => e.Email); 
        }
    }
    else
    {
        model.ContactList = contactRepository.GetAll(filter: x => x.UserId == user.Id);
    }
    

    【讨论】:

    • 感谢您的帮助。问题是我的导师说在 if 运算符中多次使用 If 运算符是不好的做法......换句话说,他告诉我不要这样做:If () { if (){ if() { 等等。 .} } }
    • @StoyanPetkov,好吧,也许您的导师应该在这里阅读答案?现实世界中没有硬性规定。我建议许多具有复合条件的else ifs 是更糟糕的做法。它的代码更多,更难阅读。
    • 好的,但在我这种情况下,我不能那样说他……但我明白你的意思,我认为你的意思是。
    • @StoyanPetkov,无论如何,我已经简化了代码,我不认为你需要在一种情况下使用OrderBy,在另一种情况下使用Sort
    • 是的,我这样做只是为了锻炼(做一件事的不同方式)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 2017-06-27
    • 1970-01-01
    相关资源
    最近更新 更多