开发者

Explaining why dual purpose classes are bad

I have a class in a system that lists its purpose as "This can either be seconds-from midnight. Or a time with a date." I have tried to explian how bad this is but I cant get my point accross. Do anyone have any ideas on how to tackle this.

ht开发者_如何学Gotp://code-slim-jim.blogspot.com/2010/10/object-anti-patterns.html


As stated this sounds like a problem with a variable. If a variable (say, float) can represent either seconds since midnight or time-and-date, then how can it be used in the code? If I want to use its value, I have to make sure I know how it was last set, and if I want to set it I must make sure I know how it well next be used.

In the larger sense I think you mean, what's wrong with having one class performing two independent tasks? The problem is that it violates encapsulation by unnecessarily exposing the implementation of one task to the implementation of another, so that a bug in one can disturb both.


I'm not sure that it's inherently wrong. I'm sure that using that class directly is an open invitation to bugs. Possibly it's an "implementation" class that can be used internally as basis for two classes, one which represents time+date, and the other representing seconds since midnight.

However, most probably it's just Wrong.

One would need a more detailed description, or the actual code, plus info about how it's being used, to decide.

Cheers & hth.,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜