Questions tagged [getters]
25 questions
202
votes
14 answers
When are Getters and Setters Justified?
Getters and setters are often criticized as being not proper OO. On the other hand, most OO code I've seen has extensive getters and setters.
When are getters and setters justified? Do you try to avoid using them? Are they overused in general?
If…
Winston Ewert
- 25,052
84
votes
12 answers
What is the utility and advantage of getters & setters especially when they are merely used to read and assign values to properties of an object?
I’m still really new to learning to program. Just learning the syntax for a few programming languages at the moment.
The courses I viewed for C# and Java touched only very briefly on getters & setters and it still didn’t make an awful lot of sense…
ProjectDiversion
- 963
15
votes
9 answers
In OOP, what counts as a "getter"
Note: I'm not looking for opinions on whether the authors of the article below are right or wrong. Mainly I'm looking for the exact definition of what they mean by getters, especially since I know some of the authors post here.
In encapsulated…
Ced
- 609
12
votes
5 answers
Is it a bad idea to use getters/setters and/or properties at all?
I am perplexed by comments under this answer: https://softwareengineering.stackexchange.com/a/358851/212639
A user is arguing there against the use of getters/setters and properties. He maintains that most times using them is a sign of a bad design.…
gaazkam
- 4,529
9
votes
4 answers
Can renaming a method preserve encapsulation?
I was reading this page, about when getters/setters are justified, and the OP gave the following code sample:
class Fridge
{
int cheese;
void set_cheese(int _cheese) { cheese = _cheese; }
int get_cheese() { return cheese; }
}
void…
QueenSvetlana
- 201
7
votes
4 answers
Encapsulation and Displaying Information
This site and SO contain many pages about getters/setters and if they break encapsulation or enforce it. My question is for those developers that agree that getters/setters break encapsulation and should be avoided.
Questions:
How do you display…
user276603
4
votes
2 answers
Should I expose an instance's state with methods?
I've read somewhere about a pattern that is as follows: if it'll change the state of the instance, the method should be named with a verb and return void; and if the method just returns something (computed or simply a property), it should be named…
4
votes
4 answers
Backing field versus private set C#
I doubted to post this question to the general StackOverflow, but it is suggested to not post opinion-based questions and this might be one. And ofcourse, this is the software engineering department. Microsoft Docs only describes the how, not the…
Jannick Breunis
- 259
4
votes
1 answer
Is using getters to exchange information between objects acceptable?
Suppose I have the following Character, Potion, and PotionType classes:
class Player:
def __init__(self, name: str, health: int, mana: int):
self._name = name
self._attributes: Dict[PotionType,int] = {}
…
user327264
4
votes
7 answers
How exactly are getters and setters defined?
Note: Questions with similar title have been asked before, but please read the full text before claiming that this is a duplicate.
Since everybody in OOP uses the terms getter and setter, I would expect they
have a well-defined meaning. But what I…
Frank Puffer
- 6,459
3
votes
4 answers
Should a class provide public mutators for all its private fields?
I work on refactoring an Java application based on a CAST audit. One of the criterion says that
To respect OO encapsulation concepts, private fields should always be
accessed through accessors
So far, so good. However, the audit highlights all…
The Once-ler
- 163
3
votes
2 answers
Dealing with a lot of getters and setters
I've already asked "Dealing with a large interface".
I have a further question regarding that situation. It was pointed out to me that I used a lot of getters and setters and so I broke encapsulation. I'm aware of this, but I can't really imagine…
Patrik Bak
- 277
2
votes
1 answer
Difference between `Class.X` and `Class.getX()`?
Might be a silly question or something I might have just messed up in my head but here we go...
I saw a code example of someone using getPos() in their own class to retrieve the current position of an object instead of for example using myObj.x and…
darclander
- 139
2
votes
4 answers
How do I avoid tightly coupling one microservice to another microservice's feature that depends on specific views of the first's data?
I've seen this problem in a few different contexts now but I'm not sure what it's called or how to think about it.
Suppose I have a service, AccountService, that serves accounts from a database,…
Andrew Cheong
- 471
2
votes
4 answers
Is using getter method violating the law of Demeter?
Suppose I have a Attendance class
public class Attendance {
private PersonInfo personInfo;
public PersonInfo getPersonInfo() {
return personInfo;
}
}
And I want to check if person is registered in the Conference class:
public class…
user3153970
- 441