Null Object Pattern

后端 未结 6 1678
执笔经年
执笔经年 2020-12-25 13:14

There seems to be a growing community of people saying that you should never return null and should always use the Null Object Pattern instead. I can see the usefullness of

6条回答
  •  执笔经年
    2020-12-25 13:53

    If you do not see the point, then it is probably not a good paradigm to use for you. The whole idea of OO programming is to make things simpler for YOU. Don't get trapped into thinking you need to adopt someone else elaborate pattern-based system. Often it takes a significant amount of work to learn various patterns and use them effectively, so it is better to grow into them, rather than try to force yourself to use it.

    As far as this particular pattern is concerned, it assumes a certain style of programming which may be inappropriate for you. I would never use it myself because I return nulls as legitimate values (missing data) which are handled differently in each case, so "centralized handling" makes no sense for me. When I return booleans, I use primitives.

    The bottom line here is that if a pattern seems unnatural to you, don't use it.

提交回复
热议问题