Allan Caine
1 min readDec 29, 2020

--

Thank you for your comment about primitive obsession. I did read the medium story below. You raise an astute point.

To avoid the problem of primitive obsession, I believe that we should have an Email class in your story’s example. The Email class should have validation behaviour. Then, we avoid any need for an extension function or a utility class which has a function accepting a String as an Email. As you correctly point out, we shouldn’t represent emails as strings.

I believe your story is reaching the wrong conclusion. If we accept that

fun String.validateEmail(): Boolean = …

is wrong, then it follows that

public static boolean validateEmail(String email) …

is also wrong, because the Kotlin resolves the extension function to byte code essentially as a static function. The extension function’s receiver becomes the first parameter of the method.

I believe that the story’s proper conclusion is that if

fun String.validateEmail: Boolean = …

is wrong because of primitive obsession reasons, then we need an Email class which has validation behaviour.

https://medium.com/the-sixt-india-blog/primitive-obsession-code-smell-that-hurt-people-the-most-5cbdd70496e9#:~:text=Primitive%20Obsession%20is%20when%20the,an%20object%20in%20a%20domain.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Allan Caine
Allan Caine

Written by Allan Caine

Senior Android Developer, TD Bank (Canada) (Opinions are my own)

Responses (1)

Write a response