c++ - Using getter/setter vs "tell, don't ask"? -


tell, don't ask principle here pasted me when use getters or setters, , people tell me not use them. site explains should , shouldn't do, doesn't explain why should tell, instead of asking. find using getters , setters more efficient, , can more them.


imagine class warrior attributes health , armor:

class warrior {     unsigned int m_health;     unsigned int m_armor; }; 

now attacks warrior special attack reduces armor 5 seconds. using setter's this:

void attacker::attack(warrior *target) {     target->sethealth(target->gethealth() - m_damage);     target->setarmor(target->getarmor() - 20);     // wait 5 seconds     target->setarmor(target->getarmor() + 20); } 

and tell, don't ask principle (correct me if i'm wrong):

void attacker::attack(warrior *target) {     target->hurt(m_damage);     target->reducearmor(20);     // wait 5 seconds     target->increasearmor(20); } 

now second 1 looks better, can't find real benefits of this. still need same amount of methods (increase/decrease vs set/get) , lose benefit of asking if ever need ask. example, how set warriors health 100? how figure out whether should use heal or hurt, , how health need heal or hurt?

also, see setters , getters being used of best programmers in world. apis use it, , it's being used in std lib time:

for (i = 0; < vector.size(); i++) {     my_func(i); } // vs. vector.execforelements(my_func); 

and if have decide whether believe people here linking me 1 article telling, not asking, or believe 90% of large companies (apple, microsoft, android, of games, etc. etc.) have made lot of money , working programs, it's kinda hard me understand why tell, don't ask principle.

why should use (should i?) when seems easier getters , setters?

you still need same amount of methods (increase/decrease vs set/get) , lose benefit of asking if ever need ask.

you got wrong. point replace getvariable , setvariable meaningful operation: inflictdamage, example. replacing getvariable increasevariable gives different more obscure names getter , setter.

where matter. example, don't need provide setter/getter track armor , health differently, single inflictdamage can processed class trying block (and damaging shield in process) , taking damage on character if shield not sufficient or algorithm demands it. @ same time can add more complex logic in single place.

add magic shield temporarily increase damage caused weapons short time when taking damage, example. if have getter/setters attackers need see if have such item, apply same logic in multiple places same result. in tell approach attackers still need figure out how damage do, , tell character. character can figure out how damage spread across items, , whether affects character in other way.

complicate game , add fire weapons, can have inflictfiredamage (or pass fire damage different argument inflictdamage function). warrior can figure out whether affected fire resistance spell , ignore fire damage, rather having other objects in program try figure out how action going affect others.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -