What is a Shim?

The term "shim" as defined in Wikipedia would technically be classified, based on its definition, as a "Structural" design pattern. The many types of “Structural” design patterns are quite clearly described in the (some would say defacto) object oriented software design patterns reference "Design Patterns, Elements of Reusable Object-Oriented Software" better known as the "Gang of Four".

The "Gang of Four" text outlines at least 3 well established patterns known as, "Proxy", "Adapter" and "Facade" which all provide “shim” type functionality. In most fields it’s often times the use and or miss use of different acronyms for the same root concept that causes people confusion. Using the word “shim” to describe the more specific “Structural” design patterns "Proxy", "Adapter" and "Facade" certainly is a clear example of this type of situation. A "shim" is simply a more general term for the more specific types of "Structural" patterns "Proxy", "Adapter", "Facade" and possibly others.


Simple Explanation via Cartoon

An example of a shim:

My Dog Ralph is one lucky bash-tard (double pun intended)

Summary

A shim is some code that takes care of what's asked (by 'interception'), without anyone being any wiser about it.

Example of a Shim

An example of a shim would be rbenv (a neat ruby tool). Calls to ruby commands are "shimmed". i.e. when you run bundle install, rbenv intercepts that message, and reroutes it according to the specific version of Ruby you are running. If that doesn't make sense try this example, or just think of the fairy god mother intercepting messages and delivering apposite outcomes.

That's it!

Important Clarifications on this example

  • Note: Like most analogies, this is not perfect: usually Ralph will get EXACTLY what he asked for - but the mechanics of HOW it was obtained is something Ralph doesn't care about. If Ralph asks for dog food, a good shim will deliver dog food.
  • Also, I didn't want to get into semantic arguments (nor needless details/complexity - e.g. Adapter gang of four design pattern and similar) so I deliberately simplified to a general concept via cartoon so you can quickly/easily get the gist - there are pros and cons with such an approach - if you prefer encyclopedic definitions then perhaps consider the Wikipedia entry on shims.

PS: RIP Ralph. A more loyal pet never existed.