“primitive obsession” or “open secret”?

Yesterday Corey Haines posted yet another great video interview with JB Rainsberger. This time the topic under discussion was the Primitive Obsession code smell — what it is, how to recognise it, why it is a problem, what to do about it, and what you’ll see when you’ve fixed it.

I agree with JB — this is a difficult concept to communicate when teaching, and I think part of that comes from the name Primitive Obsession. In fact, after a vast amount of deliberation, Bill Wake and I proposed the alternative name Open Secret in our book Refactoring in Ruby. Our reasoning is that, as JB points out in the video, the essence of this smell is the fact that the lack of an encapsulating abstraction pushes knowledge and responsibilities and behaviour out into the open; whereas the representation of that knowledge should be a secret. It’s not so much the primitives that are the problem, but that they represent a domain abstraction — and that one chosen representation of that concept is visible to, and necessarily understood by, all and sundry.

Does that rename help explain this code smell?

7 thoughts on ““primitive obsession” or “open secret”?

  1. Hi Kevin,

    Not sure about the “Open Secret” vs “Primitive Obsession” names in the context of teaching. When I first saw the term “Primitive Obsession”, a quick Google lead me to the Wikipedia article, which is actually a pretty decent explanation. I also checked out the c2 wiki and found that the “Value Object” page offers some good discussion of the issue.

    Could you explain the difficulties you have teaching it, or more importantly, the difficulty your students have understanding the concept, given its name?

    • @James – I agree that the weight of existing usage creates friction against any change of name, and that’s one of the reasons Bill & I debated for so long.

      I think one of the arguments that swayed us was that this smell isn’t about primitives, per se. For example, in Ruby you’ll see lots of code passing around arrays or hashes in which everyone knows what each field “means”, and in part it’s that loss of meaning that is the problem.

      Primitives are useful things, so the primitive itself isn’t an issue. The problem arises because the primitive is but one of the many possible representations of some domain concept; that domain concept hasn’t been named, nor has its representation been hidden, and nor has its natural behaviour been coalesced into a single place.

      We also liked the idea of harking back to Parnas’s statement that “every module should have a secret”.

      • @Kevin – thanks, that’s a very useful explanation. I can see how the term “Open Secret” actually covers wider and perhaps more important design issues than just primitive “misrepresentation”.

  2. I’m not sure “Open Secret” covers the same ground as “Primitive Obsession” for me. One of the big problems with primitive obsession is that datalumps are anonymous. X isn’t a length or an id or a count or a position, just an int with no identifying information. The term “Open Secret” seems like it contradicts that frustrating secrecy to me.

    That said, it’s nice to have a name that explains part of why this is *bad* behavior.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s