Is it better to return null or empty collection?

前端 未结 18 1934
我在风中等你
我在风中等你 2020-11-22 07:56

That\'s kind of a general question (but I\'m using C#), what\'s the best way (best practice), do you return null or empty collection for a method that has a collection as a

18条回答
  •  不要未来只要你来
    2020-11-22 08:21

    From the perspective of managing complexity, a primary software engineering objective, we want to avoid propagating unnecessary cyclomatic complexity to the clients of an API. Returning a null to the client is like returning them the cyclomatic complexity cost of another code branch.

    (This corresponds to a unit testing burden. You would need to write a test for the null return case, in addition to the empty collection return case.)

提交回复
热议问题