Skip to content
Fallow home
All docs pages

Docs home

Duplication

Find duplicated code with fallow dupes, decide which copies to merge, and set a limit for new copies.

When code exists in two places, each fix must go into each copy, and people miss one. In the demo project, summarizeCart and exportCartSummary compute the same 5 percent discount in the same 17 lines. When the discount rule changes, a fix in one file leaves the old rule in the other. fallow dupes finds the copies, so that you can decide which to merge.

npx fallow dupes

What fallow finds

● Duplicates (1 clone group)

     17 lines  2 instances  spread 2  dup:f42424e8
    src/reports/export.ts:3-19
    src/utils/summary.ts:7-23

  Duplicate code blocks
  https://fallow.tools/docs/explanations/duplication/#clone-groups

⚠ 34 lines (20.0%) duplicated across 2 files (0.02s)

Clone groups

A clone group is one block of code that exists in two or more places. Each place is an instance. Each clone group is one finding of the issue type code-duplication.

PartMeaning
17 linesThe length of the block.
2 instancesThe number of places that hold it.
spread 2How far apart the instances are, in directory steps: src/reports and src/utils are two steps apart. Fallow shows it only when it is above 0.
dup:f42424e8The handle of the group. Use it to inspect or accept the group.
34 lines (20.0%)All copies together, as a share of the code that fallow compared. Each copy counts.

Fallow puts the groups with the most to gain first: large blocks and many instances.

Clone families

Two files can share more than one block. A clone family is a set of clone groups in the same set of files. To see one, add a second copied function, which writes CSV lines, to both src/utils/summary.ts and src/reports/export.ts. Fallow then lists the family below the groups:

     17 lines  2 instances  spread 2  dup:f42424e8
    src/reports/export.ts:3-19
    src/utils/summary.ts:7-23

      8 lines  2 instances  spread 2  dup:2a315d4a
    src/reports/export.ts:23-30
    src/utils/summary.ts:27-34

● Clone families (1 with multiple groups)

  2 groups, 25 lines across src/reports/export.ts, src/utils/summary.ts
    → Extract shared function (17 lines) from export.ts, summary.ts
    → Extract shared function (8 lines) from export.ts, summary.ts

Here somebody probably copied summary.ts to make export.ts. For a family with 50 duplicated lines or more, fallow suggests one shared module for the family. For a smaller family, it suggests one function for each group.

How fallow decides

Fallow parses each file and compares the code as a sequence of tokens: names, operators, values. Whitespace, comments and formatting therefore do not hide a copy.

A block counts when it has 5 lines and 50 tokens or more, and exists in 2 places or more.

Fallow compares JavaScript and TypeScript files, Vue, Svelte and Astro files (scripts, markup and styles), and CSS, SCSS, Sass and Less files. It leaves out code that is often similar and is not worth a merge:

  • import lines, re-export lines and top-level require lines,
  • test files (*.test.*, *.spec.*, __tests__/) and mocks (__mocks__/),
  • framework caches and output: .next/, .nuxt/, .svelte-kit/, .turbo/, .parcel-cache/, .vite/, .cache/, out/ and storybook-static/.

These patterns are the default ignore list of duplication, so they apply in each run, also without production mode. A dist/ or build/ directory is not on this list. Exclude it with duplicates.ignore. To compare test files too, set duplicates.ignoreDefaults to false. Fallow then skips only the patterns of duplicates.ignore, so add the cache folders to it again.

Copies with other names

A copy often gets new names. src/reports/invoice.ts and src/reports/receipt.ts do the same work, but the default mode does not match them:

// src/reports/invoice.ts
export const buildInvoiceLines = (order: Order): string[] => {
  const lines: string[] = [];
  const subtotal = order.unitPrice * order.quantity;
  const vatAmount = Math.round(subtotal * 0.21);
  // 12 more lines
};

// src/reports/receipt.ts
export const buildReceiptLines = (purchase: Order): string[] => {
  const out: string[] = [];
  const net = purchase.unitPrice * purchase.quantity;
  const tax = Math.round(net * 0.21);
  // 12 more lines
};

The semantic mode ignores the names and the values:

npx fallow dupes --mode semantic
     17 lines  2 instances  dup:06de2df9
    src/reports/invoice.ts:4-20
    src/reports/receipt.ts:4-20
ModeIgnoresTwo blocks match when
strictWhitespace and comments.They have the same code.
mild (default)Whitespace and comments.They have the same code. mild and strict give the same result.
weakAlso the text in strings.They have the same code, or only the text in the strings differs.
semanticAlso names, strings and numbers.They have the same structure.

Of the four modes strict, mild (the default), weak and semantic, only semantic matches a copy with renamed variables. weak ignores only the text in strings. --near also matches renamed variables. Start with the default mode. The semantic mode finds more real copies, and it also finds blocks that only look alike.

Copies with small edits

The semantic mode needs the same structure, so a copy that gained or lost a line needs another check. Add --near to find functions that are at least 80 percent the same. It also ignores the names, so it finds the pair above too:

npx fallow dupes --near
     17 lines  2 instances  100% similar  dup:d3c4efc9
    src/reports/invoice.ts:4-20
    src/reports/receipt.ts:4-20

For functions that do the same job with different code, see Similar code.

Act on a finding

Before you merge two copies, look at the instances of the group. Pass the handle to --trace:

npx fallow dupes --trace dup:f42424e8
  Clone group 1  dup:f42424e8 (17 lines, 210 tokens, 2 instances, spread 2)
    >> src/reports/export.ts:3-19
    -> src/utils/summary.ts:7-23
    Suggested refactor
      Extract function, saves ~17 lines
      Proposed name: row  (best-effort, verify before applying)

The trace also prints the code of the block. A copy is not always a problem:

SituationAction
The copies do the same job, and a fix must go into each copy.Move the block to one function and call it from each place.
A clone family: the files share several blocks.Merge the files, or move the shared parts to one module. Fix the family as one task.
The copies look alike today, but belong to different features.Keep them. A shared function ties the two features together.
The copy is in generated code, such as GraphQL or OpenAPI output.Exclude the path with duplicates.ignore.
You reviewed the group and you accept it.Record its handle.

When duplication is acceptable

A merge has a cost too. Keep a copy when one of these is true:

  • The block is small, for example a guard clause or an error handler.
  • The code does not change, and it causes no bugs.
  • You keep two copies in two directories on purpose. --skip-local reports only the groups that span two directories or more.

Merge first the large blocks in files that change often. fallow health --hotspots shows these files.

Accept a reviewed group

Add the handle and the number of instances to your config file:

// .fallowrc.jsonc
{
  "duplicates": {
    "ignoredClones": ["dup:f42424e8:2"]
  }
}

Fallow hides this group. When a third instance appears, the number does not match, and fallow reports the group again.

To accept one instance only, put this comment on the line above it:

// fallow-ignore-next-line code-duplication

Fail a run

Without a gate, fallow dupes reports and exits with code 0, as in the demo run above. Two options add a gate:

# fail when more than 5 percent of the code is duplicated
npx fallow dupes --threshold 5

# fail when fallow finds at least one clone group
npx fallow dupes --fail-on-issues

The duplicates.threshold key in your config file sets the same gate for each run. The default threshold 0 turns the percentage gate off. It does not mean zero tolerance. To fail on each clone group, use --fail-on-issues. With --threshold 5, the demo run exits with code 1 and prints Duplication (20.0%) exceeds threshold (5.0%). To fail only on new clone groups, compare with a saved baseline.

In a changed-code check, a clone group that the change introduces gives a warning, not a failure. To fail on it, set the rule introduced-clone-group to error.

What fallow cannot see

  • A block below the minimum size. Lower --min-lines and --min-tokens to see small blocks, and expect more noise.
  • The same code in .ts and .js: type annotations make the two differ. Add --cross-language to remove the annotations before the comparison.
  • One group across two modes. The handle depends on the mode: the demo group dup:f42424e8 is dup:ca69f412 in semantic mode.
  • A trend across modes. Compare the duplication share only between runs with the same mode.

Reference