This question already has an answer here:
I have 2 pieces of code. What would say is best practice and why? In the first version I add a new shop and evaluate if it's successful with Exceptions, and in the second version I do the same with "if" statements.
I've recently read that Exceptions can be expensive in the code, and if all "Add" method is implemented this way it can cause performance issues. Can anybody confirm this?
Thanks!!!
public bool AddShop1(Shop newShop)
{
try
{
ShopDictionary.Add(newShop.ShopId, newShop);
return true;
}
catch (ArgumentNullException)
{
return false;
}
catch (ArgumentException)
{
return false;
}
}
public bool AddShop2(Shop newShop)
{
if (newShop == null || ShopDictionary.ContainsKey(newShop.ShopId))
{
return false;
}
ShopDictionary.Add(newShop.ShopId, newShop);
return true;
}
Aucun commentaire:
Enregistrer un commentaire