Primitive Obsession Obsession - The Code Whisperer

A Twitter conversation about Primitive Obsession caught my eye today. That conversation began with this tweet:


This is a companion discussion topic for the original entry at http://blog.thecodewhisperer.com/permalink/primitive-obsession-obsession

Interesting post. Here are a couple of old posts I found interesting:
My Favourite Smells: http://chriswheeler.blogspo...
Primitive Obsession and Balance http://www.cs.uni.edu/%7Ewa...

I have a number of ideas about this, but I'll limit myself observing that Ken Iverson's language design was all about expressing useful concepts from mathematics, concisely [and this was originally in the context of describing computing hardware - what is a memory operation, what is a register, what does "add with carry" really mean, that sort of thing - and it's something of a happy accident that it was later implemented as a programming language]. Anyways, among other things:

Searching is a primitive (actually, several primitives -- "search" can be an ambiguous term)

Sorting is a primitive (mostly - and this really deserves something blog length, describing the nuances of this part of the design).

Iteration is built into the primitives - you do not need recursion or loops in many (but not all) contexts.

These three features, alone, might represent more than half of most of the code I've seen (if we look at its purpose, rather than its details).

I hadn't thought about primitive *algorithms* here. In the realm of algorithms, I think Primitive Obsession equates to Not Invented Here Syndrome. Here, we have two competing arguments. I like to err on the side of reusing trustworthy libraries. Unless I have the explicit goal of learning from writing a library, I prefer to use an existing implementation. On the other hand, I have recently read comments like "Programmers shouldn't import 20,000 lines of code in order to avoid writing 10 lines of their own". Once again, this comment shows missing the underlying point: why do I have to import 20,000 lines of code in order to use 10? This is yet another cohesion problem. (Unrelated things are too close together.)

Regarding your specific examples, I at least prefer to work in languages (Ruby, Smalltalk, Python) where I don't have to write loops to implement those algorithms, and when I can't, I import those algorithms from a library that hides the details. Of course, I've done that for so long, that I have to make sure that it hasn't become Yet Another Ritual. :)

I'd read them both before. Thank you for the extra references.

A new one:
A problem with Primitive Obsession
http://silkandspinach.net/2...