mardi 22 septembre 2020

C# nested if-else optimization with almost similar values

I have new user data (i want to import/update it). Accounts is finded by user data. I want to update or create new accounts depended on finded accounts by user data :

            if (accountById != null)
            {
                if (accountByNumber != null)
                {
                    if (accountById.Id == accountByNumber.Id)
                    {
                        if (_isSpecialData)
                        {
                            AddUserDataToAccount(userData, accountByNumber);

                            if (userData.Status == Blocked) return;
                        }
                    }
                    else
                    {
                        _log.Error($"Bad");
                        return;
                    }
                }
                else
                {
                    AddUserDataToAccount(userData, accountById);
                }
            }
            else
            {
                if (accountByNumber != null)
                {
                    if (accountByNumber.RefNo == null)
                    {                            
                        SetAccountAdditionalId(accountByNumber, userData.AdditionalId);

                        if (_isSpecialData)
                        {
                            AddUserDataToAccount(userData, accountByNumber);
                            if (userData.Status == Blocked) return;
                        }
                    }
                    else
                    {
                        _log.Error($"Bad");
                        return;
                    }
                }
                else
                {
                    CreateCardAndProfile2(userData, out createdNewAccount);
                    CreateNewAccount(userData, out createdNewAccount);
                }
            }
                        
            UpdateAccountData(userData, createdNewAccount);

The above method works, but I would like to know if there is any way to make it more readable, optimized?

Aucun commentaire:

Enregistrer un commentaire