C
C#2w ago
Faker

✅ Importance of getters and setters

Hello, can someone explain the importance of getters and setters in programming please. I know with setters, we can check for conditions, like if age < 18, then do something but apart from that, why do we force devs to make use of setters/getters? for e.g consider: person.name = "John" why is name a private property. Why force us to use a setter instead of just making name public?
7 Replies
always say never
because this allows you for flexibility, for example to have new names for fields without touching the fields themselves, so you get in less troubles when having to change the model for a new version of the program
canton7
canton72w ago
Changing a field to a property is a breaking change: if there are other libraries which use your code, they could break. Some built-in libraries, such as WPF and some serialization code, also only work with properties. Since just using properties is very easy, lets you make things getter-only/init-only/private set, and doesn't risk breaking code if you later decide you do need a property, there's zero harm in just using properties for non-private fields from the outset
FusedQyou
FusedQyou2w ago
I know with setters, we can check for conditions, like if age < 18
Don't add any special logic to setters unless it is expected and truly necessary. If you force an age restriction on an age property, it's not clear and it will break. Porperties are not expected to throw. Use a method to set the age in that instance instead. Point of properties is accessibillity and readability, but there are plenty of other reasons, such as that it is basically the same as a method, just restricted to a specific type. Fields do not give control and it becomes confusing quickly which ones access and/or mutate the value. As a general rule you ALWAYS want to make something a property if something outside of your class is supposed to use it. From there you need to determine how much it may mutate by giving the getter or setter protection If you have a massive project you will be happy to see that you only have a check a few properties to know what accesses a class from the outside. If everything is fields you have no clue, because it might not even be the case. Let alone it might not even be allowed. Also to add to this (as an unrelated side note), the whole concept of an exception is that it throws for truly unintentional behaviour. Inserting your age to be underage is not unintentional and seems like something the user might provide as an age. Instead you should have a proper check that checks this and just have the property set the age regardless. Specific to this you could even have an enum indicating if the user is underage or mature or whatever
canton7
canton72w ago
Eh, everyone interprets "exceptional" differently. The best interpretation I heard was that an exceptional situation happens when a method can't fulfill the contract established by its name. So if a SetAge method is unable to set the age for whatever reason, that's exceptional. That aside, properties should not be throwing.
Faker
FakerOP2w ago
yeahh I see for the exceptions, we only write exceptions handling logic when the method can't hold its "promise", like say we are verifying age, if age is under 18, that's just bad data but if instead of an age, which is a number, we write eighteen, this is where we would expect exceptions to be thrown?
canton7
canton72w ago
Sure. It's still fuzzy and open to interpretation, but I think it's a good rule of thumb. Anyway, this is all irrelevant to OP's question
Faker
FakerOP2w ago
yep, I also understand why we rely on setters and getters though, thanks !

Did you find this page helpful?