特质是好还是坏?

时间:2010-07-12 05:26:57

标签: smalltalk squeak traits pharo

这是一个开放式问题,但我想征求SO社区对Traits的一些意见;你认为Squeak / Pharo中的Traits是好事,还是你应该远离它们并使用组合和代表团?我问,因为虽然我知道如何使用它们(感谢Pharo书),但我不确定使用它们是否可以接受,或者可以使用它们以及它不适用的地方。

2 个答案:

答案 0 :(得分:6)

答案 1 :(得分:1)

事情各有利弊。卢卡斯正确地提到了许多缺点:

  • 在代码中引入强大的依赖关系。
  • 没有足够的工具支持。

虽然第二天可能会消失,但第一天不会。

特征的目的是防止在两个不共享超类的超类的类共享实例方法时发生的代码重复。现在,有时授权可以解决这个问题,但通常情况下它不能。所以,特质的亲是:

  • 减少代码重复。

我的判断是,缺点超过了。我认为,今天和永远,代码重复必然会发生。当代表团不这样做时,我甚至可以想象代码重复并不是那么有害,因为它通常先于复制的代码片段的不同演变。

I think, the best thing to do, as of today, is to keep automated track of code duplication, and always monitor when one end changes while the other doesn't. I'm currently writing a tool that'll keep track of such links, even across repositories. I'll report on it in my blog when it's ready.