Mbinu za mzazi na mtoto zinapogongana!
TL;DR: Epuka kutumia mbinu za faragha katika madarasa ya wazazi zilizo na majina ambayo darasa la watoto linaweza kutumia.
Unapotumia jina la njia sawa katika madarasa ya mzazi na mtoto, unaleta mkanganyiko.
Njia ya faragha katika darasa la mzazi haiwezi kubatilishwa hata kama njia ya umma iliyo na jina sawa ipo katika darasa la mtoto.
Hili ni tatizo ambalo lugha nyingi tuli nazo katika muundo wao. Kukatwa huku kunasababisha hitilafu na kufanya msimbo wako kuwa mgumu kudumisha.
<? class ParentClass { private function greet() { // This method is private return "Hello from ParentClass"; } public function callGreet() { return $this->greet(); } } class ChildClass extends ParentClass { public function greet() { // Overriding a concrete method is a code smell // Compilers SHOULD warn you return "Hello from ChildClass"; } } $child = new ChildClass(); echo $child->callGreet(); // When callGreet() is invoked on the $child object, // it executes the following: // It calls $this->greet(), // which refers to the greet() method of ParentClass // because the original method is private // and cannot be overridden or accessed from ChildClass. // The unexpected output is 'Hello from ParentClass'
<? class ParentClass { protected function greet() { // notice the 'protected qualifier' return "Hello from ParentClass"; } public function callGreet() { return $this->greet(); } } class ChildClass extends ParentClass { public function greet() { return "Hello from ChildClass"; } } $child = new ChildClass(); echo $child->callGreet(); // The output is "Hello from ChildClass" // This is the standard (and wrong) solution // Also fixed by most AIs
<? abstract class ParentClass { // Declare greet() as an abstract method // Following the template-method design pattern abstract protected function greet(); public function callGreet() { return $this->greet(); } } class ChildClass extends ParentClass { protected function greet() { return "Hello from ChildClass"; } } class OtherChild extends ParentClass { protected function greet() { return "Hello from OtherChild"; } } $child = new ChildClass(); echo $child->callGreet(); // Output: Hello from ChildClass $otherChild = new OtherChild(); echo $otherChild->callGreet(); // Output: Hello from OtherChild
Unaweza kugundua harufu hii kwa kutafuta njia za kibinafsi katika madarasa ya wazazi na kuangalia ikiwa madarasa ya watoto yanafafanua njia zilizo na jina moja. Ni lazima pia ujaribu mbinu za mzazi kupiga simu kwa njia za faragha.
Nambari iliyo wazi na inayotabirika inapaswa kuonyesha safu ya ulimwengu halisi ambayo ni mifano.
Unapotumia njia za kibinafsi zilizo na majina yanayoingiliana, unaunda pengo la Bijection kati ya modeli na utekelezaji.
Pengo hili linachanganya wasanidi programu, huongeza kasoro, na kukiuka kanuni safi za msimbo.
Jenereta za AI mara nyingi huunda harufu hii wakati zinatengeneza uhusiano wa mzazi na mtoto.
Huenda wasiangalie viwango vya ufikiaji au kuzingatia athari za urithi.
Vyombo vya AI vinaweza kurekebisha harufu hii kwa maagizo wazi.
Unaweza kuuliza AI kuangalia kwa majina ya njia zinazoingiliana na safu za refactor.
Jaribu Them!
Kumbuka: Wasaidizi wa AI hufanya makosa mengi
Bila Maelekezo Sahihi | Pamoja na Maagizo Maalum |
---|---|
Unapounda madarasa ya wazazi na watoto, unapaswa kutumia njia zinazofafanua wazi urithi na ufikiaji.
Epuka njia za kibinafsi zinazoingiliana na mbinu za watoto. Hii huweka msimbo wako kusomeka, kupanuka, na kulinganishwa na kanuni safi za msimbo.
Lugha kama Python hukuruhusu kubatilisha njia za mzazi bila kujali majina yao, wakati Java inatekeleza viwango vya ufikiaji.
C # inatenda sawa na Java . Tofauti hizi zinamaanisha kuwa unahitaji kuelewa sheria mahususi za lugha unayofanya kazi nayo ili kuepuka tabia isiyotarajiwa.
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxviii
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xii
https://hackernoon.com/how-to- find-the-stinky-sehemu-za-your-code-part-iii-t7h3zkv
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxv
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-viii-8mn3352
Kanusho: Misimbo Harufu ni maoni yangu.
Urithi ni mzuri, lakini usisahau kamwe kuwa inaleta uunganisho mkali.
Robert C. Martin
Nakala hii ni sehemu ya Msururu wa CodeSmell kwenye HackerNoon.