Primitive Obsession

It’s useful to have a shared knowledge of different Code Smells and how to handle them. Primitive Obsession is a very common smell which occurs in projects. In this learning hour we will talk about what it is and how you are able to refactor it.

Session Outline

Connect: What is a code smell for you?

Everybody explains their own definition of a code smell.

Concept: Primitive obsession

Explain Primitive Obsession, for example:

Why is primitive obsession a disadvantage?

Ask everyone to come up with disadvantages of primitive obsession, and collect them on a whiteboard. You’re hoping for reasons like these:

Concrete: Replace primitive with object

Give a brief explanation of the process of the Replace Primitive with Object refactoring.

Using the Person Refactoring Kata, demonstrate replacing a primitive with a more complex type. For example replace the role field with a Role class.

To support the refactoring it is helpful to have some UML-like sketch on a whiteboard which shows how the target relationship between Person and other classes will look before and afterwards. Then the participants should try to refactor the class Person on their own. (In pairs or ensemble). They should use as much IDE-refactoring-support as possible.

Conclusion: How would you describe primitive obsession in your own words?

Ask everyone to explain the main idea of primitive obsession in their own words.

Acknowlegements

This learning hour was first published elsewhere: Primitive Obsession