Disclosure: When you purchase a service or a product through our links, we sometimes earn a commission.Β Read More..

FREE Email Marketing Course

Is using getters to exchange information between objects acceptable?

October 20, 2019 By admin

Software https://softwareengineering.stackexchange.com/questions/387553/is-using-getters-to-exchange-information-between-objects-acceptable

Suppose I’ve Got the following Character, Potion, along with PotionType classes:

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 potion.type, also potion.amount?

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?

Clearly, a Player will either drink the Potion and not exceed the limit, these means sharing the information (kind and amount) between the 2 classes.