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.

Filter by
Sorted by
Tagged with
46
votes
7answers
14k views

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 ...
25
votes
8answers
5k views

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 ...
23
votes
7answers
12k views

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 ...
12
votes
5answers
4k views

Is it a bad idea to use getters/setters and/or properties at all? [duplicate]

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 ...
10
votes
3answers
7k views

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;...
7
votes
4answers
961 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; } ...
7
votes
4answers
442 views

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 ...
4
votes
4answers
755 views

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 ...
4
votes
2answers
2k views

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 ...
2
votes
5answers
6k views

Is it possible to make some methods invisible/non-usable to some classes in Java?

I am developing an application in Java whereby I would like to pass an object as part of an interface into other methods of classes written by other developers in the team. The object I'm passing in ...
2
votes
2answers
2k views

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 ...
2
votes
1answer
2k views

MVVM model vs viewmodel

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 ...
1
vote
3answers
5k views

Should a getter be allowed to return a different value than was passed to the setter?

For example, given the following class: public class SomeClass { //... private IEnumerable<SomeType> myEnumerable; public IEnumerable<SomeType> MyEnumerable { ...
1
vote
2answers
2k views

Python - best way to have interdependent variable in a class

Newbie Python questions: I want to model something that can have a several inter-connected instance variables and if I set one, I want to recalculate the others. Any can be set. E.g. a circle - each ...
0
votes
2answers
1k views

Should the getters and setters of a stl container access the container itself or the elements inside it?

Consider I have a vector and map as class members: class MyClass{ protected: std::vector<int> myVector; std::map<int,std::string> myMap; }; Should the getter and setter access ...
0
votes
1answer
937 views

Concurrent and atomic updates to multiple properties/variable of an object

I have a class with multiple setters and want to make atomic updates to multiple properties/variables. As far as I can see there are three methods that could work: Call all setters in synchronized ...
0
votes
2answers
2k views

get set method naming conventions [closed]

In OOP coding, there is a long standing naming convention to name accessor/mutator methods getThing and setThing. These directly manipulate the properties, which are often private. Should these ...
0
votes
1answer
109 views

restricting access to a public setter

My question is how I can achieve more encapsulation in TypeScript. I have a class Item, with a public setter isOwned, but I only want to call this method in specific situations: if the item is picked ...
-2
votes
1answer
288 views

Set result of getter instead of setter [closed]

Is this a legit use of getter Lady lady = new Lady(); lady.getWater() = "hot water"; if we suppose getter returns Class Lady { public String getWater() { this.water; }} ?
-2
votes
1answer
566 views

Why C# ASP.NET MVC Code First POCO Classes must have getters and setters?

Why in, C# ASP.NET MVC Code First project, POCO Classes must have getters and setters ( {get; set; } ) in order to work? public float data { get; set; } or in db context class public DbSet<SGD....
-4
votes
2answers
158 views

What is the proper way to unspecify an integer's value in C++? [closed]

// Default initialization int i; // i has an unspecified value return i; // Probably 0, but Unreliable i = 5; // i has a specified value i = int();// This will give it a specified value, 0 i = ...