I have a method that has a load of if statements that seems a bit silly although I’m not sure how to improve the code.
Here’s an example. This logic was inside the view which is now in the controller which is far better but is there something I’m missing, maybe a design pattern that stops me having to check against panelCount < NumberOfPanelsToShow and handling the panelCount every condition? Maybe not, just feels ugly!
Many thanks
if (model.Train && panelCount < NumberOfPanelsToShow)
{
panelTypeList.Add(TheType.Train);
panelCount++;
}
if (model.Car && panelCount < NumberOfPanelsToShow)
{
panelTypeList.Add(TheType.Car);
panelCount++;
}
if (model.Hotel && panelCount < NumberOfPanelsToShow)
{
panelTypeList.Add(TheType.Hotel);
panelCount++;
}
...
Assuming Model.Train, Model.Car, Model.Plane are just boolean indicators as to the type of “model” (instead of you creating Train : Model, Plane : Model etc)
and in your test:
HOWEVER, since Car, PLane and Train are different, you really SHOULD have a base type Model, derive Car, Plane and Train from Model and then you can overload methods to handle each type