There’s nothing wrong with changing the get
and set
. The get
and set
are just a default name adopted in Java to access the internal information of an object.
For example, when setting a apelido
in a class Pessoa
, you may want to treat extra spaces, validate if the nickname respects the limit size, etc within the set
of the nickname. That is, you pass the responsibility to the class itself to treat this, which is perfectly valid from the point of view of object orientation.
Maybe it exists within the application in which is working a species one convention that set
and get
must return exactly the value of the fields in which they correspond. Maybe this class you changed is just to load data from side to side. Anyway, in general, this type of convention with get
and set
is quite common in projects, but there is no problem in changing their behavior, it depends even on the context in which that class is.
I had made the change of a Setter of a List. In it I did not
received a list per parameter, but an object of the type, and within the
method I made an add on the list, it saved me some lines of
code
Your idea makes perfect sense. It just seems to me to call this method set
was not the best decision. It could be a adicionar
or add
, because you’re not defining (set) a list for the class, you are adding (add) an element in the class internal list. Including, perhaps, the very method of set
of this List
nor should exist. Finally, they are just examples of how there are various situations.
Maybe it’s a duplicate: Getters and setters are an illusion of encapsulation?
– Math
@Math I believe I did not duplicate, this question refers to encapsulation, my doubt is whether it is a crime as they say make a change in the methods asuhsau advisors
– Felipe Paetzold
The rationale for not changing getters and setters is when your classes are used by third parties (for example, if they are part of a library intended to be used in other applications). You should be careful not to break the compatibility in the code that makes use of this library. Other than that there is no harm in changing getters and setters when you find that this can bring benefit to your code.
– Piovezan
The comment from @Piovezan perfectly and completely answers the question. Just one more detail: a Setter that adds the argument to a list seems strange. Suddenly this was the reason for your friend’s questioning. Not that this Setter is necessarily wrong, suddenly there’s a good reason, but maybe it’s worth it to question this particular point. As for changing a Setter, it is simply what was said by Piovezan; nothing more, nothing less.
– Caffé