Click here to Skip to main content
15,898,999 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginner in C#,could you please help simplify following code:

C#
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!
Posted
Updated 19-Jun-18 20:26pm
v2

if (AccountIdExists(accountId))
{
    var acctInfo = RetrieveAccountInformation(accountId);
    return acctInfo.AccountActive;
}
else
{
    throw new Exception("Account with account id was not found.");
}
 
Share this answer
 
This code
C#
if(acctInfo.AccountActive == true)
{
               return acctInfo.AccountActive // because this line
}
else
{
               return acctInfo.AccountActive // a,d this line are same
}

can be simplified to
C#
return acctInfo.AccountActive
 
Share this answer
 

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;

  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900