To Hell With Setters and Getters

How Accessor Methods Are a Waste of Time and Often Hinder Encapsulation

Image for post
Image for post
Alan Kay who coined the term object-oriented programming was inspired by biological cells and how they communicated when thinking about encapsulation.

Why Does It Matter?

Many will argue there is not cost to writing accessor methods (setters and getters) because modern IDEs can do it for us. Why am I making a stink about this?

struct Rocket {
int stages;
float fuel;
RocketEngine engine;
bool reusable;
};
class Rocket {
public:
Rocket();
virtual ~Rocket();

int GetStages() const;
void SetStages(int stages);
float GetFuel() const;
void SetFuel(float fuel);
RocketEngine& GetEngine();
const RocketEngine& GetEngine() const;
void SetEngine(const RocketEngine& engine);
bool GetReusable() const;
void SetReusable(bool reusable);

private:
int stages_ = 0;
float fuel_ = 0.0;
RocketEngine engine_;
bool reusable_ = false;
};
Rocket::Rocket() {

}

Rocket::~Rocket() {
}

int
Rocket::GetStages() const {
return stages_;
}

void
Rocket::SetStages(int stages) {
stages_ = stages;
}

float
Rocket::GetFuel() const {
return fuel_;
}

void
Rocket::SetFuel(float fuel) {
fuel_ = fuel;
}

RocketEngine&
Rocket::GetEngine() {
return engine_;
}

const RocketEngine&
Rocket::GetEngine() const {
return engine_;
}

void
Rocket::SetEngine(const RocketEngine& engine) {
engine_ = engine;
}

bool
Rocket::GetReusable() const {
return reusable_;
}

void
Rocket::SetReusable(bool reusable) {
reusable_ = reusable;
}
struct Point {
float x;
float y;
};

Accessors Are Not Encapsulation

At the heart of the problem with accessors is that a majority of object-oriented developers seem to think that encapsulation is simply about making sure internal data is not accessed directly.

struct Stack {
void push(int value) {
values[++top] = value;
}

int pop() {
return values[top--];
}
private:
int top = -1;
int values[CAPACITY];
};

Should You Ever Write Accessors?

By now, you might think that I am completely against ever using accessors. But that is not the point I want to make. My point is that OOP design should not be taught as stone tablet commandments which must be obeyed to satisfy the OOP gods.

Libraries Used by Third Parties

If you’re code is a library used by others, then you are more likely to need encapsulation because you can’t do a simple refactoring of customer code. However in this case, simple encapsulation through accessors might not be enough, since you often want to maintain binary backwards compatibility.

struct Point {
float x;
float y;
float z; // New variable added
};
Point points[3] = {p1, p2, p3};

You Want to Limit Access

To start off with setter and getters which do nothing, is rather pointless. However it is quite sensible to make a variable private and only provide a getter. E.g. a collection usually has a size property. You don’t want the user of your collection to be able to arbitrarily change the size property unless the number of elements have actually changed. Hence you want size to be private and provide a getter method, say getSize().

Your Type is Part of an Abstraction

In my programming language of choice, Julia, you tend to specify functions as taking abstract strings as arguments, because Julia supports many types of strings. Then you can’t access say a length variable directly, because the length of the string could potentially be calculated as needed, as with C style string, or it could be a stored variable as with Pascal. The algorithms the user writes should not have to care about this. Hence you need to provide a length() getter to abstract away this difference.

Conclusion

If you find yourself wondering whether you should write setters or getters, you got to ask yourself some questions about the code your are writing and the type (class) you are implementing. Is this for internal use only, or will customers see this type?

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store