I'm sorry, but I feel that your interpretation is of what constitutes something that *is* vs. something that *does* is incorrect. |
I'm the one who gave the rule and I don't understand what I myself said?
A mechanic is a FixEngine, or a FixWhatever. The mechanic has no purpose other than what it *does*. It does something to something else and that is all we care about. We don't care about the mechanic as a thing in any way shape or form. We only care about what it does. |
You don't have enough information about the problem that Mechanic models to make that judgement call. I'm the one who put forth the example, and I say that a Mechanic is an entity that has interest beyond the things it can do.
To give another example, consider
1 2 3 4 5
|
class Prng{
public:
Prng(unsigned seed);
unsigned next();
};
|
A Prng
does something, but it also
is something. If this wasn't the case, one Prng would be the same as any other. But because of the nature of PRNG this is clearly false. A PRNG seeded with 42 is obviously different from one seeded with 37. If you're doing random testing and you find a bug in the program when the PRNG for the input is seeded with a specific number, when you'll try to reproduce the bug you will very much care if the PRNG is seeded with that specific number again, and not with some other number.
There are plenty of examples in c++ of classes that *do* something only. A simple example is using a smart pointer ( in whatever chosen form ).
http://www.cplusplus.com/reference/memory/unique_ptr/
It's only purpose is to DO something, like taking the ownership of a pointer and destroying it in the the destructor, etc. . It is nothing in itself other than what it does. You never say... oh, I need a smart pointers because it *is* something. You use it because it *does* something, and yet it s a class. |
That's a very bad example for your argument. A std::unique_ptr<T> does something (or, more specifically, will do something upon destruction), but it also is something in its own right. For the same T, two std::unique_ptr<T> are not interchangeable.
1 2 3 4 5
|
auto s1 = std::make_unique<std::string>("cp a.txt b.txt");
auto s2 = std::make_unique<std::string>("rm -rf /");
if (rand() % 2)
std::swap(s1, s2);
system(s1->c_str());
|
At line 5, we really care whether the std::swap() call happened!
If fix_engine() is designed to clean itself up when done, then immediately calling fix_engine() again could be a perfectly valid thing to do. It's very, very short sighted to discount a very powerful mechanism such as a class, just because it *may* be used incorrectly by someone. |
You're missing my point. What I'm getting at is that if no function of a class' interface is ever capable of changing the internal state of the class, then the class could be refactored into one or more functions without losing anything. For example, if Mechanic has only one function (fix_engine()) which always does the same thing, then extracting the function from the class doesn't lose anything. As another example,
1 2 3 4 5 6 7
|
class Prng{
public:
Prng(unsigned seed){}
unsigned next(){
return 360391561;
}
};
|
can be refactored into
1 2 3
|
unsigned Prgn_next(){
return 360391561;
}
|
and nothing is lost.
However, I conceded that the Mechanic class in that example was questionable but probably acceptable.
But again, going back to the original intent of this posting, calling it like
Mechanic().fix_engine( vehicle );
would ensure that Mechanic is clean before being used. |
And would, necessarily, be equivalent to
Mechanic_fix_engine( vehicle );
, except doing it in a function would not allow someone to do
1 2 3
|
Mechanic mech;
mech.fix_vehicle(vehicle);
mech.fix_vehicle(vehicle2);
|
which might not be valid.
I feel that your interpretation of these things is well intended, but lacks deeper thought and experience. |
I don't recall ever making any personal attacks.
There is no rule, and no measurement to determine when a Mechanic class ceases to be something that *does* vs. something that *is*. |
Sure there is. A class is something if it's stateful. If it's not stateful, it doesn't need to be a class.
I have no problem with constructive criticism, but I will call out rudeness when I see it. |
I'm sorry. In the future I will try not to be rude to code. I now know sensitive software is to having its feelings hurt.