dimanche 30 novembre 2014

Should I accept empty collections in my methods that iterate over them?


I have a method where all logic is performed inside a foreach loop that iterates over the method's parameter:



public IEnumerable<TransformedNode> TransformNodes(IEnumerable<Node> nodes)
{
foreach(var node in nodes)
{
// yadda yadda yadda
yield return transformedNode;
}
}


In this case, sending in an empty collection results in an empty collection, but I'm wondering if that's unwise.


My logic here is that if somebody is calling this method, then they intend to pass data in, and would only pass an empty collection to my method in erroneous circumstances.


Should I catch this behaviour and throw an exception for it, or is it best practice to return the empty collection?





Aucun commentaire:

Enregistrer un commentaire