Questions tagged [setters]

A setter is a method used to control changes to a variable. It's most often used in object-oriented programming, in keeping with the principle of encapsulation.

They are also widely known as mutator methods.

Often a setter is accompanied by a getter (also known as an accessor), which returns the value of the private member variable.

In programming languages that support them, properties offer a convenient alternative without giving up the utility of encapsulation.

Further details:

25 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
55
votes
7 answers

Why is chaining setters unconventional?

Having chaining implemented on beans is very handy: no need for overloading constructors, mega constructors, factories, and gives you increased readability. I can't think of any downsides, unless you want your object to be immutable, in which case…
Ben
  • 1,002
  • 2
  • 10
  • 11
25
votes
8 answers

Should my sequential collection start at index 0 or index 1?

I am creating an object model for a device that has multiple channels. The nouns used between the client and I are Channel and ChannelSet. ("Set" isn't semantically accurate, because it's ordered and a proper set isn't. But that's a problem for a…
kdbanman
  • 1,447
  • 13
  • 19
24
votes
7 answers

If a variable has getter and setter, should it be public?

I have a class with a variable that is private and the class has a getter and a setter for that variable. Why not make that variable public? The only case I think you have to use getters and setters is if you need to do some operation besides the…
Oni
  • 927
  • 1
  • 9
  • 13
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
11
votes
3 answers

Stubbing Properties with private setters for tests

We have the object public class MyObject{ protected MyObject(){} public string Property1 {get;private set;} public string Property2 {get;private set;} public string Property3 {get;private set;} public string Property4…
JMan
  • 255
  • 1
  • 3
  • 8
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
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
4
votes
2 answers

Mutable objects - setters and getters

Is it a good practice to have a setter method of this kind? With primitive types, it's obviously fine, but when you have a setter for a field which holds a reference to mutable object, this might go wrong - the caller could modify the object after…
user4205580
  • 423
  • 5
  • 11
3
votes
1 answer

Should MVVM models have behaviour?

I am trying to wrap my head around MVVM and "models should not have behaviour". If my getter depends on an environment variable, should it be in the model or viewmodel? Does it make a difference if that getter is specific to one viewmodel vs…
bebbi
  • 361
  • 3
  • 8
2
votes
3 answers

Method naming conventions "setX" vs "withX"

Why learning about Fluent Interfaces, I came across this post which states that using set hints one is mutating the object whereas with is returing a new object. I have seen this pattern first hand while using PySpark (Python API for Apache…
2
votes
2 answers

How should I unit test a function that uses setters?

I'm using a repository pattern design and I've hit a stumbling block when writing a unit test for one of my methods. I'm fairly new to writing unit tests, so I would appreciate any help! Let's say I have a method which creates a product and uses…
Dan Johnson
  • 139
  • 1
  • 6
1
vote
1 answer

Calling a private method in a setter to update object at every change of the property

Code below shows setting a value of an object's property and calling a private method in a setter to update the status of the object. Is this call a good practice or setter at most should only validate incoming value and should not take part in…
apex39
  • 121
  • 1
  • 5
1
2