Resolved How do i refactor the if..else if - multiple conditions

Palak Shah

Well-known member
Joined
Apr 29, 2020
Messages
97
Programming Experience
1-3
Hello Everyone,

I have one class file where I have user if else if and with multiple conditions -However I feel class file is really confusing, so can you help me if I can refactor the code any which way?


Class File:
public static PaymentOptions GetPaymentOptions_Auto(TestConfigurationCDO testConfiguration, int siteId)
        {
            var paymentOptions = new PaymentOptions();

            var paymentOptionList = SitePaymentRepository.GetSitePaymentInfoBySiteId(
                testConfiguration,
                siteId);

            var lowestPriority = paymentOptionList.Min(x => x.Priority);
            var paymentAuto = paymentOptionList.Where(x => x.Priority == lowestPriority).FirstOrDefault();
            
            if (paymentAuto.PaymentType == PaymentMethod.Klarna)
            {
                paymentOptions = new KlarnaOptions();
            }
            else if (paymentAuto.PaymentType == PaymentMethod.PayPalDirect ||
                paymentAuto.PaymentType == PaymentMethod.Braintree ||
            paymentAuto.PaymentType == PaymentMethod.BankTransfer || paymentAuto.PaymentType == PaymentMethod.AdyenDropIn)
            {
                paymentOptions = new ClientCheckoutOptions()
                {
                    paymentMethod = paymentAuto.PaymentType
                };
            }
            else if (paymentAuto.PaymentType == PaymentMethod.PayPalExpress)
            {
                paymentOptions = new PaypalOptions();
            }
            else
            {
                paymentOptions = new PaypalOptions();
            }
            return paymentOptions;
        }
 
Solution
Switch Case helped me cleaner my code:


Updated Code:
public static PaymentOptions GetPaymentOptions_Auto(TestConfigurationCDO testConfiguration, int siteId)
        {
            var paymentOptionList = SitePaymentRepository.GetSitePaymentInfoBySiteId(
                testConfiguration,
                siteId);

            var lowestPriority = paymentOptionList.Min(x => x.Priority);
            var paymentAuto = paymentOptionList.FirstOrDefault(x => x.Priority == lowestPriority);

            PaymentOptions paymentOptions;

            switch (paymentAuto.PaymentType)
            {
                case PaymentMethod.PayPalDirect:
                case PaymentMethod.Braintree...
Switch Case helped me cleaner my code:


Updated Code:
public static PaymentOptions GetPaymentOptions_Auto(TestConfigurationCDO testConfiguration, int siteId)
        {
            var paymentOptionList = SitePaymentRepository.GetSitePaymentInfoBySiteId(
                testConfiguration,
                siteId);

            var lowestPriority = paymentOptionList.Min(x => x.Priority);
            var paymentAuto = paymentOptionList.FirstOrDefault(x => x.Priority == lowestPriority);

            PaymentOptions paymentOptions;

            switch (paymentAuto.PaymentType)
            {
                case PaymentMethod.PayPalDirect:
                case PaymentMethod.Braintree:
                case PaymentMethod.BankTransfer:
                case PaymentMethod.AdyenDropIn:
                    paymentOptions = new ClientCheckoutOptions()
                    {
                        paymentMethod = paymentAuto.PaymentType
                    };
                    break;

                case PaymentMethod.Klarna:
                    paymentOptions = new KlarnaOptions();
                    break;

                case PaymentMethod.PayPalExpress:
                default:
                    paymentOptions = new PaypalOptions();
                    break;
            }

            return paymentOptions;
        }
 
Solution
The switch expression that I linked to is not be available if you use C# 7.3 (.Net framework project), glad you found switch statement useful.
As an example I arranged the code you posted into expression, I don't know the enum or types you have, so there could be a different way of arranging it.
C#:
PaymentMethod[] checkoutGroup = { PaymentMethod.PayPalDirect, PaymentMethod.Braintree, PaymentMethod.BankTransfer, PaymentMethod.AdyenDropIn };

return paymentAuto.PaymentType switch
{
    var v when checkoutGroup.Contains(v) => new ClientCheckoutOptions() { paymentMethod = v },
    PaymentMethod.Klarna => new KlarnaOptions(),
    PaymentMethod.PayPalExpress => new PaypalOptions(),
    _ => new PaypalOptions()
};
 
The switch expression that I linked to is not be available if you use C# 7.3 (.Net framework project), glad you found switch statement useful.
As an example I arranged the code you posted into expression, I don't know the enum or types you have, so there could be a different way of arranging it.
C#:
PaymentMethod[] checkoutGroup = { PaymentMethod.PayPalDirect, PaymentMethod.Braintree, PaymentMethod.BankTransfer, PaymentMethod.AdyenDropIn };

return paymentAuto.PaymentType switch
{
    var v when checkoutGroup.Contains(v) => new ClientCheckoutOptions() { paymentMethod = v },
    PaymentMethod.Klarna => new KlarnaOptions(),
    PaymentMethod.PayPalExpress => new PaypalOptions(),
    _ => new PaypalOptions()
};
Ahh wow...I found a new way of implementation..Thank you so much for showing me this way @JohnH ?

I think I can use that too as I have version 8.0 (4.8 Framework)
 
Last edited:
@JohnH - If you have any reference sites to learn different coding patterns please do share it..I am really enjoying exploring different solutions for same problem!!!!!
 
Investing in a copies of "Effective C#" and "More Effective C#" is never a waste of money. I don't know what version the language the latest edition covers, but the past editions of the book I've bought have the same depth and quality as the excellent "Effective C++" and "More Effective C++" series of books.
 
Investing in a copies of "Effective C#" and "More Effective C#" is never a waste of money. I don't know what version the language the latest edition covers, but the past editions of the book I've bought have the same depth and quality as the excellent "Effective C++" and "More Effective C++" series of books.
Yeah so true @Skydiver
 
Not in .Net Framework, C# 8.0 is supported in .Net core 3 and .Net 5, see C# language versioning - C# Guide
If you're looking into new C# features check these articles out: What's new in C# 8.0 - C# Guide and What's new in C# 9.0 - C# Guide
I'm still confused on language version part (facepalm)

Below is my framework project file looks like, sowill it support the c#8.0 features?


Project File:
<PropertyGroup>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Framework</RootNamespace>
    <AssemblyName>Framework</AssemblyName>
    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <SkipPostSharp>True</SkipPostSharp>
    <TargetFrameworkProfile />
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>
 
No, that's and "old" framework project, last one is .Net 4.8 for that.
 
Back
Top Bottom