Settings

Theme

Primitive Obsession

acairns.co.uk

2 points by brentroose 2 years ago · 1 comment

Reader

mrkeen 2 years ago

> However, primitive types are unable to encapsulate behaviour. Because there is no way to place behaviour inside a primitive type — it is forced to exist outside:

And you end up with code reuse. I can write algorithms that accept/return String/ints, just like you can. We can even use each other's. But if MyString "encapsulates" the behaviour of Boyer-Moore search, and YourString encapsulates the behaviour of split-on-whitespace, we can no longer share algorithms.

> The lack of encapsulation prevents us from establishing a single source of truth.

The single source of truth would be a library import defining Boyer-Moore over Strings, which you and I both import. But if I implement my own BoyerMooreString (and you do too), we're both really just lying to ourselves about having the single source of truth.

> Instead of encapsulating rules and constraints within a specific type representing the concept, they are scattered throughout the codebase whenever the concept is required.

Calls to the single true implementation (as opposed to the implementations themselves) are scattered throughout the codebase whenever required. That's a system of code reuse.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection