Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct OOP design without getters?

Tags:

oop

I recently read that getters/setters are evil and I have to say it makes sense, yet when I started learning OOP one of the first things I learned was "Encapsulate your fields" so I learned to create class give it some fields, create getters, setters for them and create constructor where I initialize these fields. And every time some other class needs to manipulate this object (or for instance display it) I pass it the object and it manipulate it using getters/setters. I can see problems with this approach.

But how to do it right? For instance displaying/rendering object that is "data" class - let's say Person, that has name and date of birth. Should the class have method for displaying the object where some Renderer would be passed as an argument? Wouldn't that violate principle that class should have only one purpose (in this case store state) so it should not care about presentation of this object.

Can you suggest some good resources where best practices in OOP design are presented? I'm planning to start a project in my spare time and I want it to be my learning project in correct OOP design..

like image 585
kane77 Avatar asked Apr 01 '10 10:04

kane77


People also ask

How do you avoid getters and setters?

The simplest way to avoid setters is to hand the values to the constructor method when you new up the object. This is also the usual pattern when you want to make an object immutable.

Is getter setter necessary?

Getters and Setters play an important role in retrieving and updating the value of a variable outside the encapsulating class. A setter updates the value of a variable, while a getter reads the value of a variable.

Are getters and setters necessary C++?

Why Classes Need Getters and Setters. The convention when designing a C++ class is to make the member variables private to control access to them. With data hiding, you can write code that checks the data coming into a class to make sure it is valid before assigning it to member variables.

Should you use getters and setters within a class?

Yes, the methods of your class should call the getters and setters. The whole point in writing getters and setters is future proofing. You could make every property a field and directly expose the data to the users of the class.


2 Answers

Allen Holub made a big splash with "Why getter and setter methods are evil" back in 2003.

It's great that you've found and read the article. I admire anybody who's learning and thinking critically about what they're doing.

But take Mr. Holub with a grain of salt.

This is one view that got a lot of attention for its extreme position and the use of the word "evil", but it hasn't set the world on fire or been generally accepted as dogma.

Look at C#: they actually added syntactic sugar to the language to make get/set operations easier to write. Either this confirms someone's view of Microsoft as an evil empire or contradicts Mr. Holub's statement.

The fact is that people write objects so that clients can manipulate state. It doesn't mean that every object written that way is wrong, evil, or unworkable.

The extreme view is not practical.

like image 128
duffymo Avatar answered Sep 27 '22 17:09

duffymo


"Encapsulate your fields" so I learned to create class give it some fields, create getters, setters

Python folks do not do this. Yet, they are still doing OO programming. Clearly, fussy getters and setters aren't essential.

They're common, because of limitations in C++ and Java. But they don't seem to be essential.

Python folks use properties sometimes to create a getter and setter functions that look like a simple attribute.

The point is that "Encapsulation" is a Design strategy. It has little or nothing to do with the implementation. You can have all public attributes, and still a nicely encapsulated design.

Also note that many people worry about "someone else" who "violates" the design by directly accessing attributes. I suppose this could happen, but then the class would stop working correctly.

In C++ (and Java) where you cannot see the source, it can be hard to understand the interface, so you need lots of hints. private methods, explicit getters and setters, etc.

In Python, where you can see all the source, it's trivial to understand the interface. We don't need to provide so many hints. As we say "Use the source, Luke" and "We're all adults here." We're all able to see the source, we don't need to be fussy about piling on getters and setters to provide yet more hints as to how the API works.

For instance displaying/rendering object that is "data" class - let's say Person, that has name and date of birth. Should the class have method for displaying the object where some Renderer would be passed as an argument?

Good idea.

Wouldn't that violate principle that class should have only one purpose (in this case store state) so it should not care about presentation of this object.

That's why the Render object is separate. Your design is quite nice.

No reason why a Person object can't call a general-purpose renderer and still have a narrow set of responsibilities. After all the Person object is responsible for the attributes, and passing those attributes to a Renderer is well within it's responsibilities.

If it's truly a problem (and it can be in some applications), you can introduce Helper classes. So the PersonRenderer class does Rendering of Person data. That way a change to Person also requires changes to PersonRenderer -- and nothing else. This is the Data Access Object design pattern.

Some folks will make the Render an internal class, contained within Person, so it's Person.PersonRenderer to enforce some more serious containment.

like image 31
S.Lott Avatar answered Sep 27 '22 15:09

S.Lott