No Primitives

Credits

Inspired by Code Cop Peter Kofler

This is a Constraint - not a Kata!

Constraints can be applied to any given Kata to focus on specific techniques and / or practices. They are meant as an extreme perspective so you can clearly feel the difference to what you do usually.

Aka “No Naked Primitives”

  • All value types, that your programming language provides (e.g. booleans, numbers, strings), need to be wrapped and must not be visible at object boundaries.
  • Arrays, all kinds of containers like lists or hash-tables and even Object (the root class of the language’s class hierarchy) are considered primitive as well.

Further Explanations

Similar to Keith’s TDDaiymi this rule is designed to exercise our object orientation skills.

A string representing a name is an under-engineered design because many strings are no valid names. In an object oriented system we would like to represent the concept of a name with a Name class.

Usually Value Objects are used for this purpose. Also a list of shopping items is not a shopping basket. A general purposes list implementation offers operations that do not make sense for a shopping basket. So containers need to be encapsulated.

While it is exaggerated to wrap all primitives (see Primitive obsession obsession), I have seen too many cases of Primitive Obsessions that I rather see a few additional value objects than another map holding maps holding strings.

Image credits

Image by Rachel M. Carmena. It represents a greeting of respect and gratitude made at the beginning and at the end of a kata.