Saturday, 31 August 2013

Make method fewer lines of code

Make method fewer lines of code

I have a method like below, is there any way to refactor, cleaner way, so
that i can make it in fewer lines of code for e.g removal of if / for
loops someting like that
public void CheckProductExistThenAddToCart(CartItem item)
{
if (CartItems.Count == 0) AddToCart(item);
bool itemFound = false;
foreach (var cartItem in CartItems)
{
if (cartItem.ProductId.Equals(item.ProductId))
{
itemFound = true;
cartItem.Qty += item.Qty;
break;
}
}
if (!itemFound)
{
AddToCart(item);
}
}

No comments:

Post a Comment