Click here to Skip to main content
14,694,465 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
Can someone explain this to me?
What it means by breaking code?

The point to setters (and getters, too) is that you can change your mind later, without breaking anybody else’s code

What I have tried:

I just cant understand the meaning
Posted
Updated 26-Jun-20 7:04am

That is quite an obscure question...so I will attempt to rephrase what I think you are asking.

Why is it that a person has said to me, "getters() and setters() allow you to change your mind later without breaking anybody else's code"?

I believe they are attempting to let you know that if a the developer of a class provides getter() and setter() methods, then later the values can be altered and the code doesn't need to be changed.

Since you are mentioning them as getters() and setters() I'm guessing you are talking about Java so here is an example:

public class Animal {
  private string name;
  private int age;
  public Animal (string name, int age){
     this.age = age;
     this.name = name;
  }
}


Without the getters() and setters() the code above can only read and/or set the values of age and name when the object is constructed.

However, if you add accessor methods (getters and setters) then you will be able to set the values after the object is constructed. And you'll be able to read those values.

that is my best guess.
   
"Getters" are often unobjectionable. The proprietary language that I worked in for many years actually had a readable access control, which meant that a data member could be read without having to write a getter for it.

"Setters" are often objectionable. They can leave an object in an inconsistent state or make it difficult to evolve its behavior. For example, a change to one member often needs to coincide with a change to another member, or a change needs to be communicated to observers that implement side effects, and so on. A language that makes it easy to declare a setter isn't doing anyone a favor in my opinion. When I have a setter, I often use it even within the class itself, because setters have a habit of evolving beyond mindless one-liners that simply set the new value.
   
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900