hunt
hunt

Reputation: 321

How to simplify the IF ELSE statement C#?

if(currency.equal("CND"))
       if (string.IsNullOrEmpty(member.LastName))
                    {
                        return $"{member.FirstName}".Trim();
                    }
                    else
                    {
                        return $"{member.LastName} {member.FirstName}".Trim();
                    }

                else
                    if (string.IsNullOrEmpty(member.LastName))
                    {
                        return $"{member.FirstName}".Trim();
                    }
                    else
                    {
                        return $"{member.FirstName} {member.LastName}".Trim();
                    }

I need to simplify this statement shorter but im not sure how? Im newbie in this stuff i might need some help on it.. any suggestion?

Upvotes: 0

Views: 120

Answers (3)

cwharris
cwharris

Reputation: 18125

This is definitely over-engineered. Not shorter, but intent is clearer and it is easily extensible.

public static string GetDisplayName(Member member, string currency)
{
    return string.Join(" ", GetDisplayNameParts(member, currency));
}

public static IEnumerable<string> GetDisplayNameParts(Member member, string currency)
{
    switch (currency)
    {
        case "CND":
            yield return member.LastName ?? ""
            yield return member.FirstName ?? ""
            yield break;

        default:
            yield return member.FirstName ?? ""
            yield return member.LastName ?? ""
            yield break;
    }
}

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66439

You can move some of your code around to cut out the duplication. If the last name is missing, the first name is the only one to print, so test for that first. Then test for the condition that prints out the full name in the preferred order.

if (string.IsNullOrEmpty(member.LastName))
{
    return member.FirstName.Trim();
}
else
{
    return currency.equal("CND")
        ? $"{member.LastName} {member.FirstName}".Trim()
        : $"{member.FirstName} {member.LastName}".Trim();
}

You could rewrite it as a nested ternary operation too, though it's no shorter really and whether it's more readable depends on the person reading it...

return (string.IsNullOrEmpty(member.LastName)
        ? member.FirstName
        : currency.equal("CND")
          ? $"{member.LastName} {member.FirstName}"
          : $"{member.FirstName} {member.LastName}").Trim();

Upvotes: 1

Leo
Leo

Reputation: 14820

This does not necessarily simplifies it, it simply makes it a one-liner

return $"{(!member.LastName.IsNullOrEmpty() ? member.LastName : "")}{member.FirstName}".Trim();

However, for better clarity and readibility the if/else block is perfectly fine...

        if (string.IsNullOrEmpty(member.LastName))
        {
            return $"{member.FirstName}".Trim();
        }
        else
        {
            return $"{member.LastName} {member.FirstName}".Trim();
        }

I would definitely prefer the if...else block to using a one-line string interpolation

Upvotes: 3

Related Questions