实例字段是否需要前缀?

看到一篇文章 Prefixing Instance Fields?,作者遇到的问题是如果使用前缀来标明 instance field(实例字段) 的名称所隐含的意思,作者给出了两个解决方案:

1.The first is to use as few fields as possible.... Do not use fields unless the data you want to store in them are an inherent part of the object and the data needs to hang around for a while.

首先使用尽可能少的实例字段。除非你需要把数据存储在实例字段中以保持长时间的访问或者是继承,不要过多使用实例字段。如果一定要传递数据,那么可以引入 method parameter 参数或者 introduce a parameter object 参数对象。

2.The second practice is too keep your methods short and focused.

第二条就是尽可能让你的方法简短并且集中。如果一个方法一次只做一件事情,那么就可以保持简洁,那么就不会出现需要很多个实例对象的情况。

posted on 2009-10-27 00:23  zhaorui  阅读(311)  评论(0编辑  收藏  举报

导航