What is a Shim?

TerminologyDefinitionVocabularyShim

Terminology Problem Overview


What's the definition of a Shim?

Terminology Solutions


Solution 1 - Terminology

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.

Solution 2 - Terminology

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.

Solution 3 - Terminology

According to Microsoft's article "Demystifying Shims":

> It’s a metaphor based on the English language word shim, which is an > engineering term used to describe a piece of wood or metal that is > inserted between two objects to make them fit together better. In > computer programming, a shim is a small library which transparently > intercepts an API, changes the parameters passed, handles the > operation itself, or redirects the operation elsewhere. Shims can also > be used for running programs on different software platforms than they > were developed for.

So a shim is a generic term for any library of code that acts as a middleman and partially or completely changes the behavior or operation of a program. Like a true middleman, it can affect the data passed to that program, or affect the data returned from that program.

The Windows API is an example:

> The application is generally unaware that the request is going to a > shim DLL instead of to Windows itself, and Windows is unaware that the > request is coming from a source other than the application (because > the shim DLL is just another DLL inside the application’s process).

So the two programs that make the "bread" of the "shim sandwich" should not be able to differentiate between talking to their counterpart program and talking to the shim.

What are some pros and cons of using shims?

Again, from the article:

> You can fix applications without access to the source code, or without > changing them at all. You incur a minimal amount of additional > management overhead... and you can fix a > reasonable number of applications this way. The downside is support as > most vendors don’t support shimmed applications. You can’t fix every > application using shims. Most people typically consider shims for > applications where the vendor is out of business, the software isn’t > strategic enough to necessitate support, or they just want to buy some > time.

Solution 4 - Terminology

As for origins of the word, quoth Apple's Dictionary widget

noun
   a washer or thin strip of material used to align parts, 
   make them fit, or reduce wear.

verb ( shimmed, shimming) [ trans. ]
   wedge (something) or fill up (a space) with a shim.

ORIGIN early 18th cent.: of unknown origin

This seems to fit quite well with how web designers use the term.

Solution 5 - Terminology

Shims are used in .net 4.5 Microsoft Fakes framework to isolate your application from other assemblies for unit testing. Shims divert calls to specific methods to code that you write as part of your test

Solution 6 - Terminology

Another Example of a Shim

When I was a kid, my grand mother lived in the same town. It was a small town. Everyone knew each other.

I used to write letters to her:

To:   Grand Ma
From: Ben

and post it into the post box..........the letters used to get delivered to her, without fail.

I didn't know or care how it happened.

Q: This begs the question: how did the letters get delivered?

A: That's easy: the messages were shimmed. Because it was a small town, the guy in the post office knew who wrote the letter (Ben from Woop Woop), and knew the address of Grandma, and so altered and shimmed the address so that the message could be delivered correctly to her.

# shimmed msg thanks to Post Office operator
To:   Mrs Ammama Kutty, 1 PA Avenue.... 
From: Ben

It's the same basic concept, except applied to code. You send a message. That message is interecepted, and the result is as expected. That's a shim.

Solution 7 - Terminology

As we could see in many responses here, a shim is a sort of adapter that provides functionality at API level which was not necessarily part of that API. This thread has a lot of good and complete responses, so I'm not expanding the definition further.

However, I think I can add a good example, which is the Javascript ES5 Shim (https://github.com/es-shims/es5-shim):

Javascript has evolved a lot during the last few years, and among many other changes to the language specification, a lot of new methods have been added to its core objects.

For example, in the ES2015 specification (aka ES5), the method find has been added to the Array prototype. So let's say you are running your code using a JavasScript engine prior to this specification (ex: Node 0.12) which doesn't offer that method yet. By loading the ES5 shim, these new methods will be added to the Array prototype, allowing you to make use of them even if you are not running on a newer JavaScript specification.

You might ask: why would someone do that instead of upgrading the environment to a newer version (let's say Node 8)?

There is a lot of real cases scenarios where that approach makes sense. One good example:

Let's say you have a legacy system that is running in an old environment, and you need to use such new methods to implement/fix a functionality. The upgrade of your environment still a work in progress because there are compatibility issues that require a lot of code changes and tests (a critical component).

In this example, you could try to craft your own version of such functionality, but that would make your code harder to read, more complex, can introduce new bugs and will require tons of additional tests just to cover a functionality that you know it will be available in the next release.

Instead, you can use this shim and make use of these new methods, taking advantage of the fact that this fix/functionality will be compatible after the upgrade, because you are already using the methods known to be available in the next specification. And there is a bonus reason: since these methods are native to the next language specification, there is a good chance that they will run faster than any implementation that you could have done if you tried to make your own version.

Another real scenario where such approach is welcome is at browser level. Let's say you need to support old browser and want to take advantage of these newer features. Javascript is a language that allows you to add/modify methods in its core objects (like adding methods to Array prototype), and those shim libraries are smart enough to add such methods only if the current implementation is lacking of them.

PS:

  1. You will see the term "Polyfill" related to these Javascript shims. Polyfill is a more specialized type of shim that is used to provide forward compatibility in different browser level specifications. By the way, my example above refers exactly to such example.

  2. Shims are not limited to this example (adding functionality that will be available in a future release). There are different use cases that would be considered to be a shim as well.

  3. If you are curious about how this specific polyfill is implemented, you can open Javascript Array.find specs and scroll to the end of the page where you will find a canonical implementation for this method.

Solution 8 - Terminology

SHIM is another level of security check which is done for all the services, to protect upstream systems. SHIM Server validates every incoming request, with Headers User credentials, against the user credentials, which are passed in the request(SOAP / RESTFUL).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNanookView Question on Stackoverflow
Solution 1 - TerminologyBenKoshyView Answer on Stackoverflow
Solution 2 - TerminologyDougView Answer on Stackoverflow
Solution 3 - TerminologyRichie ThomasView Answer on Stackoverflow
Solution 4 - TerminologyThiloView Answer on Stackoverflow
Solution 5 - TerminologyMichael FreidgeimView Answer on Stackoverflow
Solution 6 - TerminologyBenKoshyView Answer on Stackoverflow
Solution 7 - TerminologyDavid Rissato CruzView Answer on Stackoverflow
Solution 8 - TerminologyNirbhay RanaView Answer on Stackoverflow