Say it once and only once
TL;DR: Avoid duplicate email validations.
Problems Addressedhttps://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
StepsIdentify where email validation logic is duplicated.
\
Create an Email Address class to encapsulate validation rules.
\
Refactor code to use the Email Address class instead of raw strings.
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 |
Tagshttps://maximilianocontieri.com/refactoring-007-extract-class?embedable=true
https://maximilianocontieri.com/refactoring-012-reify-associative-arrays?embedable=true
https://hackernoon.com/improving-the-code-one-line-at-a-time?embedable=true
CreditsImage by Gerd Altmann on Pixabay
This article is part of the Refactoring Series.
All Rights Reserved. Copyright , Central Coast Communications, Inc.