DEV Community

Cover image for Unhealthy Code: Primitive Overuse

Unhealthy Code: Primitive Overuse

James Hickey on August 30, 2019

One of the classic "code smells" is called Primitive Overuse. It's deceptively simple. Note: This is an excerpt from my book Refactoring TypeS...
Collapse
 
stealthmusic profile image
Jan Wedel • Edited

There are a couple of good examples here! One thing that I don’t like about the enum is that it mixes two concerns:

  1. Authentication Result (Success, Failure), there are really only those two
  2. User State, might even allow more than one (FirstLogin, LockedOut) etc

I would probably use a type like a tuple that returns something like
{Success, [FirstTime]}
{Failure, [LockedOut]}
Just a quick scratch, but you might the idea.

Collapse
 
sleeplessbyte profile image
Derk-Jan Karrenbeld

The canonical term you'll find in other documents including research papers is Primitive Obsession.

Smells usually don't tell us that something is wrong, but only indicate their might be something stinky. Overuse sounds very emotive to me.

Great article idea

Collapse
 
jamesmh profile image
James Hickey

Yes, I wanted the book to be a bit more approachable and less academic-like, so I took some liberties with the official terms for most code smells.

Thanks!

Collapse
 
sleeplessbyte profile image
Derk-Jan Karrenbeld

Ah. That makes sense to me!

I'm personally not a fan of this specific term (whereas I like the other conventions you've forgone πŸ”₯πŸ’) because the term is judgy and opinionated.

Keep writing 🍻

Collapse
 
antontsvil profile image
Anton T

Any reason we wouldn't use switch/case with the strategy pattern?

Collapse
 
jamesmh profile image
James Hickey

You can. If there many different cases then I just find this technique cleaner.

Collapse
 
ben profile image
Ben Halpern

Great post

Collapse
 
jamesmh profile image
James Hickey

Thanks Ben.

Collapse
 
selbekk profile image
selbekk

Hm... cool patterns! And thanks for sharing parts of your book for free. Definitely buying it 😘

Collapse
 
jamesmh profile image
James Hickey

Awesome!

Collapse
 
techwekk profile image
Tijana Wekker

As a student this is helpful because we donβ€˜t practice enough hands-on to develop smarter codings. Thanks for sharing!

Collapse
 
jenbutondevto profile image
Jen

hehe, I am a swift/iOS developer by preference, (web) fullstack developer by trade. I’ve spent a long time wishing j/typescript had a pretty way of doing guard statements, and widely adopted.