E bolele gatee le gatee feela
TL;DR: Phema ditiišeletšo tša imeile tše di ipoeleditšego.
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
Hlaola moo logiki ya netefatšo ya imeile e phetoletšwago gona.
Hlama sehlopha sa Email Address
go akaretša melawana ya netefatšo.
Refactor khoutu go šomiša sehlopha sa Email Address
go e na le dithapo tše tala.
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; } }
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; } }
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.
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.
Ntle le Ditaelo tše di Swanetšego | Ka Ditaelo tše di Itšego |
---|---|
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