So everyone who cares about best practices in OOP and keeping code clean and in OOP knows that methods shouldn't be doing more than one thing. Methods are discrete units that do one thing and get out.
But here's a situation though where you could save some processing and improve performance if you were to combine 2 methods which are really doing 2 things into one and reuse the existing for loop that you already have in the first method:
private void RemoveDiscontinuedItems()
{
for(int s = 0; s < itemList.Count; s++)
{
if(!itemList[s].ItemIsOnSite)
{
RemoveItem(itemList[s].Id); // remove it from the DB
itemList.RemoveAt(s); // remove it from the collection
s--;
}
}
}
private void RemovePriceChangedItems()
{
for (int s = 0; s < itemList.Count; s++)
{
if(!PricingOptionIsValid(itemList[s]))
{
RemoveItem(itemList[s].Id); // remove it from the DB
itemList.RemoveAt(s); // remove it from the collection
s--;
}
}
}
These are called at page load. One removes items that are discontinued. The other removes items that have some pricing options that have changed and removes them from the same list.
Now if we were to stick with best practices, one could say that these are 2 completely independent purposes, thus we should not combine the logic in both these methods. That would then make the method be doing 2 things and I'd also have to come up with some f'd up name like RemoveDiscontinuedAndPriceChangeItems() or a generic name that doesn't tell me jack sh** like RemoveInvalidItemsFromList():
private void RemoveDiscontinuedItems()
{
for(int s = 0; s < itemsList.Count; s++)
{
if((!itemList[s].ItemIsOnSite))
{
RemoveItem(orderItemsSavedList[s].Id); // remove it from the DB
itemList.RemoveAt(s); // remove it from the collection
s--;
}
else if(!PricingOptionIsValid(itemList[s]))
{
RemoveItem(itemList[s].Id); // remove it from the DB
itemList.RemoveAt(s); // remove it from the collection
s--;
}
}
however thinking about the performance side, calling 2 methods that are looping through the same list to remove some items, would be more costly in cycles.
So, anyone against combining or are you for combining in this situation? Would like to hear some opinions out there.