views:

110

answers:

2

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.

http://code-slim-jim.blogspot.com/2010/10/object-anti-patterns.html

+1  A: 

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.

Beta
A: 

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.,

Alf P. Steinbach
Thats how I actually plan to avoid the issue if I cant talk them down. Ill derive the object into 2 kids and the reinterpret cast it into the child type to enforce the Date+time or just Time state, then i can use operator+, operator> in a sane manor.
Ashley Smart
@Ashley: isn't that your argument then? "If the class has more than one meaning, I can't use operator+ and operator > in a sane manner". Sounds like a pretty good point to me.
jalf