# Code Smell 178 - Subsets Violation

> TL;DR: Create Small objects and restrict your domain.

# Problems

- [Bijection](https://maximilianocontieri.com/the-one-and-only-software-design-principle) Fault 

- [Fail fast principle violation](https://maximilianocontieri.com/fail-fast)

- [Repeated Code](https://maximilianocontieri.com/code-smell-46-repeated-code) validation

# Solutions

1. Create small objects and validate the domain.

# Context

This is a primitive obsession smell.

*EmailAddresses* are a subset of *string*.

*Valid Ages* are a subset of *Real*.

*Ports* are a subset of *Integers*.

A *wordle word* is a subset of String.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/6fedc83479a0894ca2467247ecd5e85c)
```java
destination = "destination@example.com"
  
destination = "destination.example.com"
// No error thrown
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/51ea6966c13b5aa25c34437218eef5b8)
```java
public class EmailAddress {
    public String emailAddress;

    public EmailAddress(String address) {
        string expressions = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
        if (!Regex.IsMatch(email, expressions) {
          throw new Exception('Invalid address');
        }
        this.emailAddress = address;
    }
}

destination = new EmailAddress("destination@example.com");
```

Not to be confused with the anemic [Java version](http://officedev.github.io/ews-java-api/docs/releases/api-2.0/apidocs/microsoft/exchange/webservices/data/property/complex/EmailAddress.html)

# Detection

[X] Manual

This is a semantic smell.

# Tags

- Primitive Obsession

# Conclusion

We need to be loyal to the bijection of the real world. 

Subsets are very important for early validations and fail fast principle.

# Relations

%[https://maximilianocontieri.com/code-smell-122-primitive-obsession]

# More Info

%[https://maximilianocontieri.com/fail-fast]

# Disclaimer

Code Smells are just my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Mona Eendra](https://unsplash.com/@monaeendra) on [Unsplash](https://unsplash.com/s/photos/boxed?)  

* * *

> Every craftsman starts his or her journey with a basic set of good-quality tools.

_Andrew Hunt_

%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
