Suppose I have the following Character
, Potion
, and PotionType
classes:
class Player:
def __init__(self, name: str, health: int, mana: int):
self._name = name
self._attributes: Dict[PotionType,int] = {}
self._attributes[PotionType.Health] = health
self._attributes[PotionType.Mana] = mana
def replenish(self, potion):
if potion.type in self._attributes:
self._attributes[potion.type] = potion.amount
class 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 main concern here is the replenish(self, potion)
method in the Player
class. Is it bad practice for my character object to ask for the potion.type
, and potion.amount
?
I'm aware of the Tell, Don't Ask guideline, but even the author admits that there is a time and place for getters:
One thing I find troubling about tell-dont-ask is that I've seen it encourage people to become GetterEradicators, seeking to get rid of all query methods. But there are times when objects collaborate effectively by providing information.
and this link:
but I fear that just telling people to avoid getters is a rather blunt tool. There are too many times when objects do need to collaborate by exchanging data, which leads to genuine needs for getters.
Is this one of those situations when there is a genuine need for a getter?
Obviously, a Player
will drink the Potion
and not exceed the limit, these means sharing the data(type
and amount
) between the two classes.