paint-brush
Refactoring 019 - Tsela ya go Reify Diaterese tša Imeileka@mcsee
1,052 dipuku tša go balwa
1,052 dipuku tša go balwa

Refactoring 019 - Tsela ya go Reify Diaterese tša Imeile

ka Maximiliano Contieri4m2024/12/05
Read on Terminal Reader

Nako e telele kudu; Go bala

Phema ditiišeletšo tša imeile tše di ipoeleditšego.
featured image - Refactoring 019 - Tsela ya go Reify Diaterese tša Imeile
Maximiliano Contieri HackerNoon profile picture

E bolele gatee le gatee feela

TL;DR: Phema ditiišeletšo tša imeile tše di ipoeleditšego.

Mathata ao a Rarolotšwego

Khoutu ye e Amanago Monkgo

https://hackernoon.com/kamoo-o ka-hwetšago-dikarolo-tše-kgabo-tša-karolo-ya-go-khoutu-x-i7r34uj

https://hackernoon.com/kamoo-o ka-hwetšago-dikarolo-tšeo-tše-nkgogago-tša-khoutu-ya-go-karolo-xxv

https://hackernoon.com/kamoo-o ka-hwetšago-dikarolo-tšeo-tše-ntlegago-tša-khoutu-ya-go-karolo-xiv

https://hackernoon.com/kamoo-o ka-hwetšago-dikarolo-tšeo-tše-nkgogago-tša-khoutu-ya-go-karolo-xxxvi

Khoutu Monkgo 20 - Pele ga nako Optimization

Mehato

  1. Hlaola moo logiki ya netefatšo ya imeile e phetoletšwago gona.


  2. Hlama sehlopha sa Email Address go akaretša melawana ya netefatšo.


  3. Refactor khoutu go šomiša sehlopha sa Email Address go e na le dithapo tše tala.

Khoutu ya Mohlala

Pele ga

 public class Person { private String emailAddress; // Primitive Obsession public void setEmailAddress(String emailAddress) { // Duplicated code if (!emailAddress.matches( "^[\\w.%+-]+@[\\w.-]+\\.[a-zA-Z]{2,}$")) { throw new IllegalArgumentException( "Invalid email address format"); } this.emailAddress = emailAddress; } } public class JobApplication { private String applicantEmailAddress; public void setApplicantEmailAddress(String emailAddress) { // Duplicated code if (!emailAddress.matches( "^[\\w.%+-]+@[\\w.-]+\\.[a-zA-Z]{2,}$")) { throw new IllegalArgumentException( "Invalid email address format"); } this.applicantEmailAddress = emailAddress; } }

Morago ga

 public class EmailAddress { // 2. Create an `EmailAddress` class to encapsulate validation rules. private final String value; public EmailAddress(String value) { // The rules are in a single place // And all objects are created valid if (!value.matches("^[\\w.%+-]+@[\\w.-]+\\.[a-zA-Z]{2,}$")) { throw new IllegalArgumentException( "Invalid email address format"); } this.value = value; } } public class Person { private final EmailAddress emailAddress; public Person(EmailAddress emailAddress) { // 1. Identify where email validation logic is duplicated. // 3. Refactor code to use the `Email Address` // class instead of raw strings. // No validation is required this.emailAddress = emailAddress; } } public class JobApplication { private EmailAddress applicantEmailAddress; public JobApplication(EmailAddress applicantEmailAddress) { this.applicantEmailAddress = applicantEmailAddress; } }

Mohuta

  • [x] Seka-Go Itiriša

Polokego

Refactoring ye e bolokegile ge o tšeela legato ditiragalo ka moka tša dithapo tša imeile tše tala ka sehlopha sa 'EmailAddress' gomme o netefatša gore diteko ka moka di a feta.

Ke ka Baka La’ng Molao o Le Kaone?

O dira gore netefatšo ya imeile e se fetoge go ralala le tirišo ya gago.


Ka ge melao ya netefatšo e tsentšwe bogareng lefelong le tee, khoutu e ba bonolo go e hlokomela.


Gape o fokotša kotsi ya diphošo tšeo di bakwago ke tlhaologanyo yeo e sa dumelelanego.


Lefaseng la nnete, Email Addresses ke dilo tše nnyane tšeo di lego gona ebile ga se dithapo.


Khoutu yeo e fetotšwego e kgauswi le MAPPER ya lefase la kgonthe .


Hlokomela gore maina a bijection a bohlokwa. Go tla thuša go hlama EmailAddress , e sego Email , ka ge Imeile e swanetše go mmapa go molaetša wa nnete.


O se ke wa dumelela Premature Optimizators go go botša gore tharollo ye e na le kotlo ya tshepedišo.


Ga ba ke ba dira ditekanyetšo tša kgonthe ka ya data ya kgonthe ya lefase.

Refactor Ka AI

Ntle le Ditaelo tše di Swanetšego

Ka Ditaelo tše di Itšego

Moqoqo GPT

Moqoqo GPT

Claude

Claude

Go gakanega

Go gakanega

Mokgweetsi wa sefofane

Mokgweetsi wa sefofane

Gemini

Gemini

Dithegi

  • Go khupetša

Di-Refactoring tše di Amanago

Dikrediti

Seswantšho ka Gerd Altmann go Pixabay


Sehlogo se ke karolo ya Letoto la Refactoring.

Tsela ya go Kaonafatša Khoutu ya Gago ka Di-Refactoring tše Bonolo

L O A D I N G
. . . comments & more!

About Author

Maximiliano Contieri HackerNoon profile picture
Maximiliano Contieri@mcsee
I’m a sr software engineer specialized in Clean Code, Design and TDD Book "Clean Code Cookbook" 500+ articles written

HANG TAGS YA GO FEGA

ARTICLE YE E HLAHILWE KA...