Return to site

CODE SMELL: Primitive obsession 😱

· coding,code-smell

It happens when developers avoid creating their own fundamental types to deal with domain behavior or complex logic (for example, currency 🪙, telephone numbers ☎️, addresses📬), and rely too much on primitives. This does not leverage any of the principles of information hiding and encapsulation and can result in very poor 👎maintainability.

Why is it bad? 🤔

This leads to poor encapsulation since knowledge about how to handle such domain behavior gets fragmented 🪓 throughout the code.

This can very easily lead to duplication of knowledge when having to deal with validation ✅ and other operations.

It's easy to forget 🤦♂️ to update parts of the knowledge when the intended behavior changes.

How to fix it? 🔧

Model domain behavior and non-primitive knowledge through custom types 🆔 .

Beware of overfitting 💪 types to their use cases. This can lead to "type explosion" 💥, which also leads to hard-to-maintain code. "Type explosion" happens when we create too many fundamental types which differ from each other only slightly, indicating we are missing the correct underlying abstractions.


All Posts

Almost done…

We just sent you an email. Please click the link in the email to confirm your subscription!