paint-brush
Msimbo Harufu 286 - Mbinu ya Mzazi na Mtoto Huingilianakwa@mcsee
Historia mpya

Msimbo Harufu 286 - Mbinu ya Mzazi na Mtoto Huingiliana

kwa Maximiliano Contieri4m2025/01/16
Read on Terminal Reader

Ndefu sana; Kusoma

Migongano ya njia za kibinafsi katika madarasa ya mzazi na mtoto inaweza kusababisha hitilafu na harufu ya kanuni. Epuka hili kwa kuweka majina wazi, kuepuka mwingiliano, na kufuata mbinu safi za kanuni katika madarasa ya wazazi zenye majina ambayo darasa la watoto linaweza kutumia.
featured image - Msimbo Harufu 286 - Mbinu ya Mzazi na Mtoto Huingiliana
Maximiliano Contieri HackerNoon profile picture
0-item

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.

Matatizo

  • Ukiukaji mdogo wa kanuni ya mshangao
  • Tabia na kasoro zisizotarajiwa
  • Vitegemezi vilivyofichwa
  • Upanuzi mdogo
  • Utata wa kanuni
  • Ukiukaji wa kanuni ya wazi/iliyofungwa
  • Ubunifu unaopotosha

Ufumbuzi

  1. Epuka madaraja
  2. Badilisha jina la njia za kibinafsi
  3. Dumisha jina wazi
  4. Epuka kuingiliana kwa majina
  5. Epuka njia zinazolindwa
  6. Tawisha kwa mahusiano muhimu , sio kutumia tena msimbo

Muktadha

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.

Sampuli ya Kanuni

Si sahihi

 <? 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'

Sawa

 <? 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

Ugunduzi

  • [x] Nusu-Otomatiki

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.

Lebo

  • Utawala

Kiwango

  • [x] Kati

Kwa Nini Upinzani Ni Muhimu

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.

Kizazi cha AI

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.

Utambuzi wa AI

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

GumzoGPT

GumzoGPT

Claude

Claude

Kuchanganyikiwa

Kuchanganyikiwa

Copilot

Copilot

Gemini

Gemini

Hitimisho

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.

Mahusiano

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.

Credits: Picha na Matt Artz kwenye Unsplash


Urithi ni mzuri, lakini usisahau kamwe kuwa inaleta uunganisho mkali.

Robert C. Martin


Nakala hii ni sehemu ya Msururu wa CodeSmell kwenye HackerNoon.