paint-brush
Refactoring 019 - How to Reify Email Addressesby@mcsee
1,058 reads
1,058 reads

Refactoring 019 - How to Reify Email Addresses

by Maximiliano ContieriDecember 5th, 2024
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

Avoid duplicate email validations.
featured image - Refactoring 019 - How to Reify Email Addresses
Maximiliano Contieri HackerNoon profile picture

Say it once and only once

TL;DR: Avoid duplicate email validations.

Problems Addressed

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-x-i7r34uj

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

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxvi

Code Smell 20 - Premature Optimization

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;
    }
}

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;
    }
}

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

Credits

Image by Gerd Altmann on Pixabay


This article is part of the Refactoring Series.

How to Improve Your Code With Easy Refactorings