Questions tagged [getters]
The getters tag has no summary.
25 questions
4
votes
2
answers
2k
views
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 (...
15
votes
9
answers
6k
views
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 ...
2
votes
1
answer
451
views
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 ...
2
votes
4
answers
2k
views
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, e.g.
...
-2
votes
2
answers
769
views
Why use private access modifier if we need to access private variables anyway?
Everywhere is said that a private member can only be accessed from inside the same class, but at the same time, we can access the private member using getters and setters, therefore my question is:
...
2
votes
4
answers
1k
views
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 ...
84
votes
12
answers
31k
views
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? [closed]
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 & ...
4
votes
4
answers
4k
views
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. ...
2
votes
2
answers
429
views
The usage of getter notation inside the context of the class
Consider the following JavaScript code:
class MyClass {
#myPrivateField;
constructor() {
#myPrivateField = new AnotherClass();
this.theGetter.method1(); // or: this.#myPrivateField....
9
votes
4
answers
1k
views
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; }
...
3
votes
4
answers
2k
views
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 ...
2
votes
1
answer
712
views
Should I use accessors or public static fields for global constants?
I have to work on some code that was CAST-audited. The report says that it is bad in Java to use public static and that accessors should be preferred. That is also what I was taught at school.
The ...
0
votes
1
answer
599
views
use always get and set methods is a bad practique, is call directly an attribute class a bad practique in OOP?
I have a doubt about if there are some recommendations for call directly an attribute in a class
I think that in OOP you always should call an attribute by the get method.
For example:
On set
...
4
votes
1
answer
244
views
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: ...
0
votes
0
answers
2k
views
Are groovy automatic getters and setter effectively any different to public variables?
To provide a very blunt example (as I am at work and can't currently think of a sensible example).
If I write a groovy class like this
class Wendy{
byte[] frank
String doSomethingWithFrank(){...