12. August 2018
One of the most important aspects in refactoring the most effective way is looking out for Primitive obsession.
What is it?
- The usage of primitive types instead of utilising small objects to represent data.
- Use of constants for the setting of information such as role=1
- Overuse of primitive types can make extending functionality more complicated and introduce more tech debt.
Why is this a problem?
Primitive obsession can come about from programming laziness and short cutting. If I want to store a title of a book in my class, then why not just use a string? Its much easier than creating a new object?
As you add more properties to the class, then things start to introduce more noise and the class become larger and harder to read.
How to treat this smell?
With lots of primitive types being introduced to the class one way to try and resolve this is to try to group them into logical groups in their own class. Examples of grouping can be something like functionality, behaviour or domain.
Code will become more flexible than using lots of primitive types, and creates a better understanding and organisation of code. It also makes it easier to find duplicated code when they are grouped into the same area of the code base.