+ 7
What is the point of getters and setters?
Seriously, I just don't get it. If a variable is public, how could someone manipulate that variable so it corrupts the data or something? Aren't you still changing the variable value when you use getters and setters? Why do you need getters and setters when you can just refer to a public variable of a class?
19 ответов
+ 13
They are more like 'official' ways to get and set values of a variable and you're right, it doesn't make sense when the aforementioned variable is public because you can already access it. Like:
myClass.myVar = 10;
or
cout << myClass.myVar;
But, and that is a big but, let's suppose this myClass.myVar is actually a balance for some random bank account, like myAccount.balance. And as the manager of the said bank, you wouldn't want a completely random individual changing it like:
myAccount.balance = 1500000; // in dollars
or
myAccount.balance = 0; // Let's say this is your account and some random dude comes in and changes it to 0
It would make more sense if one can't change balance directly but with some methods that also do something else (like logging these transfer or withdrawal actions) or just see the balance. That is where the setters and getters come in, to provide some methods to get and set this kind of values.
To summarize:
- Yes, it doesn't make sense to use getters and setters if the variable is public
- But if the variable is private and you want other people to access it with the way YOU want it, you need to use them.
Hope I was clear and this helps.
+ 11
It makes it easier to read.
Sometimes you want the programmer to see a vraible but not set a variable. Like when you get an IP address. You can see the IP address but not set the IP address.
This is useful to control your classes according to how it's meant.
Don't ask why it's used, ask what it's benifits are.
+ 10
Getter and setter just makes your code more CLEAN and makes debugging for large program easy
+ 7
It's mainly about managing complex code, it has nothing to do with allowing someone running the application to manipulate variables if that's what you were thinking (because it's what I thought when I just got into programming, started with C#.)
Here are some other good reasons:
https://stackoverflow.com/questions/1568091/why-use-getters-and-setters/1568230#1568230
And in this video, Brackeys shows some valid uses of getters and setters (properties) as if you were coding a game:
https://www.youtube.com/watch?v=gvQziNULkdY
+ 4
When you start getter and setter are not that important.
Create a project and enjoy the proces of making something.
When your project gets bigger you find the need of organising your code and that is the moment that objects come in to your code.
With objects also come property's.
start with standard getter and setter, so your code is prepared for anything you want to do in the future.
string _myProperty { get; set; }
// you can use this in the same way a you use fields. You will find out in the future, you also can do a lot more.
You can make properties read-only or perform some kind of check in the getter or setter.
I like the bank-account example which is used in a lot of tutorials.
You have
A bank-account
where the balance is a read-only property (the user is not allowed to changed the value directly he needs methods to do that)
you can withdraw money from the bank-account using a method withdraw
you can add money to your bank-account using a method deposit
+ 4
In my opinion, the main reason is to allow you to change the underlying implementation without changing the API and to isolate access to the underlying data. If your code is a sufficiently modular (meaning different pieces can be used in different places), giving direct access to the variables pretty much forces you to use an implementation with those variables. If you ever change this, you may have to update our break all of the clients that use those variables. This is more important if you are involved in a bigger project because a small project likely has control of all of the callers using a class.
Example:
if I expose a list of transactions directly in an Account class, I'm committing to storing them that was. In contrast, a getter or setter allows me to use some other data structure underneath (perhaps a call to a database) if I ever need to change it.
class Account {
public:
List<Transaction> transactions;
}
class AccountWithGetters1 {
public:
List<Transaction> getTransactions();
void setTransactions(List<Transaction> trans) {
...
}
}
I can change what happens in the "..." part with the getter/setter system.
With all of that said, it's probably a bad design to use getters/setters too much. You should only expose the mutability that you need for the class. For example, maybe just an addTransaction(Transaction t) method and a getTransactions() method.
+ 4
just consider it as file permissions:
getters: read only
setters: write only
public: do as you want
+ 2
coders consider setters and getters irrelevant, but developers always know that what ever code they write today should be revisited by another developer someday.
When you use getters and setters, you don't have to put a comment to warn future developers that a variable or method is meant to be used only within the class.
Without getters and setters, the resources of a class cannot be controlled accordingly.
+ 1
Getters and Setters mainly used for Encapsulation, so that it provides security.
0
Get & set turns a field or a variable into a property and allows to create a copy of the class..then based on the rules you set you can make it read only (like a book) or read and write (like a notebook) and you could also set rules what can be written into the notebook etc..
0
it's useful when you want to restrict or control access to reading and writing over a variable.
for example. You have a Class Student with property age.
you might want to restrict that the user can't. use an negative age.
so you use a restriction (if) inside the setter refusing to set the age if value < 0.
0
Simply they are return and set methods, its shorthand for making method with better encapsulation for returning that value. On sololearn its declared badly, read about it on docs.microsoft.com.
0
protecting your object's values, organisation, and triggering additional behaviour within the get/ set methods
0
Probably its just one concept of applying encapsulation in programming. =)
0
I use it mainly to add/control logic and restrictions. You might want to validate the setter, and maybe even alter it in the getter. A public variable can work just fine depending on the design
0
Also you can do things such as late loading or validating inputs before setting etc...
0
It's purpose it to have clean, customizable and dynamic variables which can have some level of protection as well through encapsulation.
0
h
- 2
Nice one