Given:
private void UpdataFieldItems(List<FieldItemBase> existingFieldItems, List<FieldItemBase> selectedFieldItems)
{
List<FieldItemBase> newFieldItemsSelected;
var fieldSelectionChanges = GetFieldSelectionChanges(out newFieldItemsSelected);//retuns a Flagged enum
if (Coding.EnumHas(fieldSelectionChanges, FieldSelectionChanges.AddedFieldItem))
{
StartEditMode();
SetColumnDescriptorsToAdd(newFieldItemsSelected);
UpdateItems(selectedFieldItems);
SetColumnsToShow();
CustomizeAlignmentAndCellFormatters(_tfaTableGrid.TableGrid);
if (_tfaTableGrid.TableGrid.ColumnDescriptors.Count() > 0)
{
SetAdditionalFirstGroupedColumn();
}
StopEditMode();
}
else if (Coding.EnumHas(fieldSelectionChanges, FieldSelectionChanges.Summary))
{
StartEditMode();
UpdateItems(fieldItems);
SetColumnsToShow();
if (_tfaTableGrid.TableGrid.ColumnDescriptors.Count() > 0)
{
SetAdditionalFirstGroupedColumn();
}
StopEditMode();
}
else if (Coding.EnumHas(fieldSelectionChanges, FieldSelectionChanges.Order) ||
Coding.EnumHas(fieldSelectionChanges, FieldSelectionChanges.AddedCustomFieldItem) ||
Coding.EnumHas(fieldSelectionChanges, FieldSelectionChanges.RemovedItem))
{
UpdateItems(fieldItems);
SetColumnsToShow();
}
Invalidate();
}
//Coding.cs
public static bool EnumHas(FieldSelectionChanges selectionChanges, FieldSelectionChanges valueToCheck)
{
return (selectionChanges & valueToCheck) == valueToCheck;
}
I am willing to refactor the above code. There are two things that i don't like about the code above:
1) Writing same method calls in different cases, its not being possible to pull out the common method calls from these cases.
2) The readability of this code is very bad. It would be very confusing to understand and debug, if later needed.
Can someone suggest a design pattern for this code? or some way to improve upon the above two concerns?
Thanks for your interest.