It is very borderline. I'm not fond of the unit tests you'd have to write.
I would rather see a method on the BasketItem which reads
public bool isInBasket(int GroupID, DateTime? ArtworkDate)
{
return this.GroupID == GroupID &&
this.ArtworkDate == ArtworkDate ?? this.ArtworkDate;
}
And then
/// <summary>
/// Is a group in the basket already?
/// </summary>
public static bool isItemInBasket(List<BasketItem> BasketItems, int GroupID)
{
return isItemInBasket(BasketItems, GroupID, null);
}
public static bool isItemInBasket(List<BasketItem> BasketItems, int GroupID, DateTime? ArtworkDate)
{
return BasketItems.Any(c => c.isInBasket(GroupID, ArtworkDate)) != null;
}
I might go further and put an interface on BasketItem so that I can put fake responses on these tests.
All that said, I wouldn't fail you on a code review for your code, I'd just warn you that if it got more complicated then it would be a problem.
Edit: Actually, reading that back, I've just noticed that it demonstrates the point. Because now it doesn't make sense. This forces me to reconsider my naming.
public bool isInGroup(int GroupID, DateTime? ArtworkDate)
{
return this.GroupID == GroupID &&
this.ArtworkDate == ArtworkDate ?? this.ArtworkDate;
}
And then
/// <summary>
/// Is a group in the basket already? (do I even need this comment any more?)
/// </summary>
public static bool isGroupInBasket(List<BasketItem> BasketItems, int GroupID)
{
return isItemInBasket(BasketItems, GroupID, null);
}
public static bool isGroupInBasket(List<BasketItem> BasketItems, int GroupID, DateTime? ArtworkDate)
{
return BasketItems.Any(c => c.isInGroup(GroupID, ArtworkDate)) != null;
}
Further Edit: Actually, the further we go down this road, the more I think I see the problem. Everyone, myself included, who feels it is duplicated is reacting to the method overload, which instinctively should be one method calling another.
But in reality, the second method is doing something very, very different. And the fact that none of us can understand what that ArtworkDate field is for suggests that maybe the name of the second method should be changed. isGroupInBasketAnd...What?
I think that you see the code as non-duplicated because you know how different it is, but everyone else looks, sees the same name and thinks it's just a standard overload, a bit of syntactic sugar.
BasketItems.Any(c => c.GroupID == GroupID), with the same result, right? – nikie Aug 24 '11 at 16:24.Any()with no other changes actually gives you the cleanest final solution. – Toby Aug 24 '11 at 21:59