This is my OnAppearingAsync method where I as a page load in Xamarin I filter data, group it together, set data based on it, order the groups e.t.c. However the code is rather long and looks a bit messy, I've tried to refactor it myself but I always get stuck on where I'm supposed to start.

How can I improve this? Everything works beautifully and efficiently as far I know, I just believe it would never pass any code review in current status (We don't have code review though)

public async Task OnAppearingAsync(string houseCode)
            HouseCode = houseCode;

            var client = AppClientHelper.Get();

            var groupHolders = new ObservableCollection<SubBrandGroup>();

            // Get The Groups
            List<SubBrandViewModel> subBrandViewModels = client.GetSubBrandViewModels(HouseCode);

            // Filter the subBrands
            var filteredSubBrands = subBrandViewModels.Where(item => CurrentCustomerCardViewModel.AllowedCategories.AllowedSubBrands.Contains(item.Code)).ToList();

            foreach (var subBrandViewModel in filteredSubBrands)
                var basicItems = client.GetBaiscItemViewModelsBySubGroup(subBrandViewModel.HouseCode, subBrandViewModel.Code);

                // Apply the proper price, unique customer values e.t.c
                foreach (var basicItem in basicItems)
                    basicItem.PropertyChanged += OnOrderCountUpdate;
                    await SetUniqueBrandStatusBrandStatus(basicItem);

                // Order Item in SubGroup by alphabetical order
                var sortedItems = basicItems.OrderBy(item => item.Description).ToList();

                SubBrandGroup subBrand = new SubBrandGroup(subBrandViewModel.Name, true, subBrandViewModel.AdditionalCollateralItem, sortedItems);

                // Add group if not empty
                if (subBrand.Count != 0)

                Currency = basicItems.FirstOrDefault()?.Currency;

            if (groupHolders.Count == 0) return;

            // Order the groups
            int[] map = { 0, 2, 5, 3, 1, 4 };

            // Sort the group
            var sortedGroups = groupHolders.OrderBy(x => map[(int)x.AdditionalCollateralItem]).ToList();

            // Assign the new sorted items to the list
            ProductList = new ObservableCollection<SubBrandGroup>(sortedGroups);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.