Skip to content

Shim Overview

In software development, shimming (often spelled "shim") refers to the practice of introducing a piece of code that intercepts or adapts the behaviour of existing code — typically to provide compatibility, apply fixes, or modify behaviour without changing the original source.

Polyfills vs. Shims

A polyfill is a type of shim that replicates newer behaviour in older environments.

But shims can also intercept, modify, or fake functionality, not just add missing features.

Dependency Injection / Runtime Interception

In back-end or compiled languages (e.g., C#, Java), a shim might:

  • Replace a real implementation with a mock or proxy.
  • Inject behavior for logging, telemetry, or testing.

Testing

Shims are commonly used to mock static methods, sealed classes, or unmockable code.

Libraries like Microsoft Fakes (in Visual Studio Enterprise) let you shim .NET methods.

Example

Here is an example in C#. The original code may look something like this:

// Original untestable code
public static class TimeProvider {
    public static DateTime Now => DateTime.Now;
}

The Shimmed version could look like this:

// Shimmed version for testing
public interface ITimeProvider {
    DateTime Now { get; }
}

public class DefaultTimeProvider : ITimeProvider {
    public DateTime Now => DateTime.Now;
}

public class TestTimeProvider : ITimeProvider {
    public DateTime Now => new DateTime(2000, 1, 1);
}

Summary

A Shim is a thin layer of code used to adapt, replace, or simulate behaviour. These are typically used for compatability fixes, testing, API adapting, or runtime patching.