Questions tagged [getters]

22 questions
83
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
  • 953
  • 1
  • 4
  • 6
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
  • 3,517
  • 3
  • 19
  • 35
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…
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
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
  • 119
  • 1
  • 7
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
4 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,411
  • 5
  • 21
  • 38
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
  • 7
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
  • 1
  • 7
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…
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,…
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
  • 361
  • 1
  • 2
  • 9
2
votes
2 answers

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.method1(); this.theGetter.method2(); // or:…
goodUser
  • 129
  • 3
2
votes
2 answers

OOP in Java - What can getters be used for?

Usually, getters always return the value of a variable. I learned in my literature that access to fields is controlled by getters and setters. When I had my code rated by programmers, it was suddenly said that getters and setters violate…
Henry Weinert
  • 211
  • 2
  • 10
2
votes
2 answers

Is there any reason to avoid private getters?

Even though I could write something like this (In C#. There are, of course, equivalents in other languages): public int SomeNumber { private get; set; } I have never encountered something like this. Is there any particular reason? Edit: The…
Michael Haddad
  • 2,651
  • 3
  • 17
  • 27
1
2