Background:
I have two lists: one with a list of workcentres and the other with a list of the stations in the centres.
This code matches the stations to the centres by comparing the stations' workcentre number property with the name property of the centres.
private void WsSorter(WorkcentreList wcl, List<WorkstationCard> wscl)
{
// Find index of workcentres
foreach (Workcentre wc in wcl.WcList)
{
foreach (WorkstationCard wsc in wscl)
{
if (!(wsc.WorkcentreNo == MAIN_WORKCENTRE_1 ||
wsc.WorkcentreNo == MAIN_WORKCENTRE_2 ||
wsc.WorkcentreNo == MAIN_WORKCENTRE_3 ))
{
wc.CardList.Add(wsc); // Must be adding to "Other WC"
}
// Add to matching workcentre
else if (wsc.WorkcentreNo == wc.WcName)
{
wc.CardList.Add(wsc);
}
}
}
}
Question
The lint tool I am using, SonarLint
is telling me off for violating the DRY principle in the else if
condition.
It works, albeit with poor error checking but I would like to know, is there a better way I could write the code / approach the situation, especially regarding not repeating the line wc.CardList.Add(wsc);
?
Additional context: There are 3 main workcentres. All other workcentres (of which there is an arbitrary amount) should be classified under 'other'. I set the code up such that if the stations don't belong to a main workcentre, it must belong in the 'other' workcentre.