Home > Web Front-end > JS Tutorial > Refactoring - Reify Email Addresses

Refactoring - Reify Email Addresses

Barbara Streisand
Release: 2024-12-25 16:50:15
Original
844 people have browsed it

Sayit once and only once

TL;DR: Avoid duplicate email validations.

Problems Addressed

  • Repeated email validation logic in multiple places.
  • Risk of inconsistent validation rules.
  • Difficult to maintain validation rules.
  • Bijection violation
  • Primitive Obsession
  • Premature Optimization

Related Code Smells

Refactoring  - Reify Email Addresses

Code Smell 46 - Repeated Code

Maxi Contieri ・ Dec 8 '20

#oop #codenewbie #tutorial #webdev
Refactoring  - Reify Email Addresses

Code Smell 122 - Primitive Obsession

Maxi Contieri ・ Mar 17 '22

#oop #webdev #tutorial #beginners
Refactoring  - Reify Email Addresses

Code Smell 66 - Shotgun Surgery

Maxi Contieri ・ Apr 5 '21

#codenewbie #tutorial #oop #webdev
Refactoring  - Reify Email Addresses

Code Smell 177 - Missing Small Objects

Maxi Contieri ・ Nov 5 '22

#webdev #javascript #beginners #programming
Refactoring  - Reify Email Addresses

Code Smell 20 - Premature Optimization

Maxi Contieri ・ Nov 8 '20

#oop #developing #coding #codesmell

Steps

  1. Identify where email validation logic is duplicated.
  2. Create an Email Address class to encapsulate validation rules.
  3. Refactor code to use the Email Address class instead of raw strings.

Sample Code

Before

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;
    }
}
Copy after login

After

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;
    }
}
Copy after login

Type

[X] Semi-Automatic

Safety

This refactoring is safe if you replace all occurrences of raw email strings with the 'EmailAddress' class and ensure all tests pass.

Why is the Code Better?

You make email validation consistent across your application.

Since validation rules are centralized in one place, the code becomes easier to maintain.

You also reduce the risk of bugs caused by inconsistent logic.

In the real world, Email Addresses are small objects that exist and are not strings.

The refactored code is closer to the real world MAPPER

Notice that bijection names are essential. It would help to create an 'EmailAddress', not an 'Email', since the Email should map to the actual message.

Don't let Premature Optimizators tell you this has a performance penalty.

They never do actual benchmarks with real world data.

Refactor with AI

Without Proper Instructions With Specific Instructions
ChatGPT ChatGPT
Claude Claude
Perplexity Perplexity
Copilot Copilot
Gemini Gemini

Tags

  • Encapsulation

Related Refactorings

Refactoring  - Reify Email Addresses

Refactoring 007 - Extract Class

Maxi Contieri ・ Jul 4 '22

#webdev #beginners #javascript #tutorial
Refactoring  - Reify Email Addresses

Refactoring 012 - Reify Associative Arrays

Maxi Contieri ・ Nov 19 '23

#webdev #programming #beginners #php
Refactoring  - Reify Email Addresses

Refactoring 002 - Extract Method

Maxi Contieri ・ Nov 25 '21

#refactoring #oop #webdev #codenewbie

Credits

Image by Gerd Altmann on Pixabay


This article is part of the Refactoring Series.

Refactoring  - Reify Email Addresses

How to Improve your Code With easy Refactorings

Maxi Contieri ・ Oct 24 '22

#webdev #beginners #programming #tutorial

The above is the detailed content of Refactoring - Reify Email Addresses. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template