Is using getters to exchange information between objects acceptable?October 20, 2019
Suppose I’ve Got the following
Potion, along with
Course Player: Name = name self. _features: Dict[PotionType,int] = self. _attributes[PotionType.Health] = wellness self. Attributes: self. _features [potion.type] = potion.amount course PotionType(Enum): Health = 1 Mana = 2 class Potion: def __init__(self, amount: int, type: PotionType): self.amount = amount self.type = type def refill(self, amount): pass
My principal concern here is that the
replenish(self, potion) method in the
Player class. Is it bad habit for the personality thing to ask for your own
I am aware of the Tell, Do not Ask guideline, but even the author admits that there is a time and place for getters:
Is I’ve seen it
Encourage folks to become GetterEradicators, wanting to get rid of
all procedures that are query. But there are occasions when things collaborate by supplying advice.
And this link:
However, I fear that people is a fairly blunt
tool. When things do have to collaborate with exchanging data, which leads to real needs for getters, there are too many times.
Is this one of the situations when is a need for a getter?
Player will either drink the
Potion and not exceed the limit, these means sharing the information (
amount) between the 2 classes.