Reuse good -- Abstractions better!

I posted something about the word 'Upsert' recently -- and I was kind of surprised by the comments.

My real feelings about the concept of coining a new term 'upsert' are simple:

Don't do it!

Upsert is a bad name. An evil name. And not because it is a silly portmanteau.

Coining a term upsert, to embody the concept of "Update or Insert" is bad because it fundamentally misses the point of "Why We Code".

Let's get back to basics

Just say you discover that your code is frequently peppered with this little snippet:

If (this.State != New) {
this.Update();
} else {
this.Insert();
}

So -- you decide to extract that code snippet and place it in its own routine.

Routines are nice, routines are great.

"Aside from the invention of the computer, the routine is arguably the single greatest invention in computer science."
  Why You Should Use Routines, Routinely

This is what you create:

private void Upsert() {
If (this.State != New) {
this.Update();
} else {
this.Insert();
}
}

Now -- any instance of those fives lines becomes instead:

  Upsert();

Great! You've achieved one of the primary goals of routine construction: code reuse. Your code is now shorter, and thus hopefully cheaper to maintain. It's more versatile because now if you change the way "update or insert" works you only need to change it in one place.

Life is sweet. The birds sing and the flowers bloom once more.

But there's something amiss. Something foul remains.

You've failed on the other goal of routine construction -- you've failed to hide away the implementation details. You've failed to "raise the abstraction level!"

The user/programmer still has to think about the inner concepts: update and insert. Every time they see that words "upsert" -- the details "Update and Insert" are staring them in the face.

You lose sleep. Your dreams are troubled. Dark clouds follow you everywhere. And in quiet moments you hear the ominous echo of Alfred North Whitehead whispering:

"Civilization advances by extending the number of important operations we can perform without thinking."

--Alfred North Whitehead

Did you read that? What? You missed it -- here it is again:

"Civilization advances by extending the number of important operations we can perform without thinking."

--Alfred North Whitehead

By choosing the name "Upsert" we've failed in our duty to advance civilization! We want the user (the programmer who uses your routine) to perform these two important operations without thinking about both of them.

Let's think of a better name -- a name that alleviates the consumer from having to think about the constituent parts of the routine they're using.

How about this: Save.

private void Save() {
If (this.State != New) {
this.Update();
} else {
this.Insert();
}
}

Isn't that nicer? And civilization has taken another tiny step forward.


Incidentally -- it seems this 'Alfred North Whitehead' was a functional Programmer in the 1800s -- before Church and the Lambda calculus that gave rise to functional programming. Here's a comment about him:

"There are no fundamental "things," or "objects" in the world of Whitehead. Whitehead's ontology, or parts-list of the universe, contains only processes."
--Richard Lubbock in 'Alfred North Whitehead: Philosopher for the Muddleheaded'

 

My book "Choose Your First Product" is available now.

It gives you 4 easy steps to find and validate a humble product idea.

Learn more.

(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.