Originally Posted by
Sebastiani
>> I don't think he was proposing exposing member variables. Pure design says that a class should only expose operations. Getters and setters expose state, not operations.
Well, to be sure, he wasn't clear on that. Even so, to say that get/set functions are a sign of a poor design because they expose state is a moot point. All functions essentially get or set the state of something. But I do agree that "functions that expose unnecessary state information" should be avoided.
In general, classes should model behavior, structs should model data. Unfortunately, we see a lot of beginners who think classes are simply promoted structs with the appropriate getters and setters. So instead of ...
Code:
class Car {
public:
void drive();
};
... we see ...
Code:
class Car {
public:
void setSteeringWheel (SteeringWheel sw);
SteeringWheel getSteeringWheel();
void setSparkPlug(SparkPlug sp);
SparkPlug getSparkPlug();
...
};