您能帮助简化/修复以下代码吗? [英] Can you help simplify/fix following code

查看:68
本文介绍了您能帮助简化/修复以下代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的初学者,请你帮忙简化以下代码:



I am a beginner in C#,could you please help simplify following code:

public class AccountInfo
{
               public int AccountId { get; set; }
               public string AccountName { get; set; }
               public bool AccountActive { get; set; }
}

private static bool IsAccountActive(Guid accountId)
{
               if(accountId == null)
               {
                              throw new Exception("Account id is null.");
               }
               else
               {
                              if(AccountIdExists(accountId) == true)
                              {
                                             AccountInfo acctInfo = RetrieveAccountInformation(accountId);
                                             if(acctInfo.AccountActive == true)
                                             {
                                                            return acctInfo.AccountActive
                                             }
                                             else
                                             {
                                                            return acctInfo.AccountActive
                                             }
                              }
                              else
                              {
                                             throw new Exception("Account with account id was not found.");
                              }
               }
}

private static bool AccountIdExists(Guid accountId)
{
}





我尝试了什么:



您能否帮助简化/修复以下代码并解释其中的问题谢谢!



What I have tried:

Can you help simplify/fix following code and explain what is the problem in it.Thank you!

推荐答案

if (AccountIdExists(accountId))
{
    var acctInfo = RetrieveAccountInformation(accountId);
    return acctInfo.AccountActive;
}
else
{
    throw new Exception("Account with account id was not found.");
}


此代码

This code
if(acctInfo.AccountActive == true)
{
               return acctInfo.AccountActive // because this line
}
else
{
               return acctInfo.AccountActive // a,d this line are same
}



可以简化为


can be simplified to

return acctInfo.AccountActive


您可以通过重新排列 if 来避免所有 else 语句声明。此外,如果帐户ID不存在,我只会返回 false 。通过抛出异常并在代码中的其他位置捕获它们来控制程序流并不是一个好主意。我的建议是值得这样的。

You can avoid all the else statements by rearranging your if statements. Also, I would simply return false if the account Id does not exist. It's not a good idea to control program flow by throwing exceptions and catching them elsewhere in your code. My suggestion, for what it's worth, would be something like this.

private static bool IsAccountActive(Guid accountId)
  {
    if (accountId == null)
     {
      throw new ArgumentException("Parameter cannot be null", "accountId");
     }

    if (!AccountIdExists(accountId))
     {
       return false;
     }
    AccountInfo acctInfo = RetrieveAccountInformation(accountId);
    return acctInfo.AccountActive;

  }


这篇关于您能帮助简化/修复以下代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆