CRP

Common Reuse Principle

“Don’t force users of a component to depend on things they don’t need.” – Robert C. Martin

This is the Interface Segregation Principle for Components. Classes/Modules that are reused together belong together.

When to use

When splitting a large package. If users only use 10% of your package, it’s too big.

Why it matters

  • Efficiency: Users don’t download/install massive libraries just for one utility function.
  • Dependencies: If I depend on your “Utils” package for a string function, but “Utils” also depends on “HeavyImageProcessingLib”, I now strictly depend on that heavy lib for no reason.

Signs of Violation

  • Importing a huge library just to use one helper function (import { clone } from 'massive-lodash-like-lib').
  • Users complaining about bundle size because of your library.

Explanation

Problem

Dependencies are transitive. If A depends on B, and B contains unrelated C, A implicitly depends on C.

Solution

Split components. Put things that are used together in the same place. Separate things that are not.

Real world analogy

Cable TV bundles. You want to watch Sports, but you are forced to pay for (depend on) Cooking and Gardening channels. You want to just buy the Sports Channel.

Pros and Cons

Pros Cons
  • Leaner dependencies
  • Faster installs
  • More packages to manage (fragmentation)
  • Comparison

    • ISP: CRP is ISP applied to packages.
    • CCP: CCP says “Group things together”; CRP says “Split things apart”. You must balance them.

    Code example

    Typescript

    Bad (Violation)

    Good (Adherence)

    PHP

    Bad (Violation)

    Good (Adherence)