Reuse a Shared Rule

Change the pass rule in one authoritative place

A class report and a student badge both decide whether a mark passes. If each contains its own copy of the same threshold, a policy change can update one and leave the other inconsistent.

DRY means “Don’t Repeat Yourself”: keep one authoritative representation of shared knowledge or a shared rule. Reusing a function can prevent duplicated policy, but similar-looking code is not always the same policy.

  1. Find shared knowledge

    Identify a rule that must stay consistent across callers.

  2. Reuse appropriately

    Parameterize the varying data while centralizing the rule.

  3. Avoid false sharing

    Keep independent school policies independent.

Two callers depend on one shared pass rule

  1. Callers

    Report and badge ask the same question.

    Different student marks
  2. Shared rule

    One function defines the threshold.

    mark >= 25
  3. Results

    Each caller receives its own answer.

    true or false
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

Share the rule while keeping callers independent

hasPassed accepts a mark, so it can work for different students. Its threshold lives in one place rather than being repeated in every report. This is different from copying the function into several files.

Reuse is valuable when callers share the same reason for change. A pass rule and an attendance award may coincidentally use the same number today but belong to different policies. Joining them can create accidental coupling: changing one wrongly changes the other.

Before starting, review reusable parameters and results. These examples use JavaScript; other languages can use different syntax and rules.

Call one rule from different school tasks

The shared function returns false for 20 and true for 35. Both callers use the same passMark. If the school revises this shared policy, its authoritative definition can be changed once.

In a real multi-file project, export the shared rule from a module instead of maintaining copies. The module lesson shows actual import and export syntax.

JavaScript · local teaching example

const passMark = 25;
function hasPassed(mark) {
  return mark >= passMark;
}
console.log("Report: " + hasPassed(20));
console.log("Badge: " + hasPassed(35));

Expected output

Report: false
Badge: true

Keep independent rules explicit

Passing and receiving an excellence award are different questions. Each function has a clear threshold and name, even though both use >=. Reusing the comparison operator alone would not centralize meaningful shared knowledge.

JavaScript · local teaching example

function hasPassed(mark) { return mark >= 25; }
function earnsExcellence(mark) { return mark >= 40; }
console.log(hasPassed(35));
console.log(earnsExcellence(35));

Expected output

true
false

Apply the rule to a small change

Centralize the average calculation and call it for Maya and Ravi.

Worked answer

Only the input marks vary. One calculateAverage definition implements the common equal-weight rule and returns 37.5 for Maya and 35 for Ravi.

JavaScript · local teaching example

function calculateAverage(maths, science) {
  return (maths + science) / 2;
}
console.log(calculateAverage(40, 35));
console.log(calculateAverage(28, 42));

Expected output

37.5
35

Recognize mistakes and limits

Do not create a large general-purpose helper just to eliminate two similar lines. An abstraction with many flags and unrelated responsibilities can hide the rules it was meant to clarify.

Shared code creates a shared impact when changed. Test every meaningful caller against the intended policy, especially boundary values.

A reusable function needs a documented input and failure contract. Centralizing an unchecked operation does not automatically make it correct; decide where validation belongs.

For the language or tool rules used here, see JavaScript function interfaces.

Check the core idea

Which duplication is most important to remove here?
Choose one answer. You can try again.

Answer and explanation

Copies of the same pass threshold in callers that must agree.

Correct. These represent one shared rule that can drift.

Apply the idea to a new situation

Why might one helper with many unrelated flags be a poor DRY improvement?
Choose one answer. You can try again.

Answer and explanation

It can couple unrelated policies and make each task harder to understand.

Correct. Removing text duplication can increase conceptual complexity.

Centralize knowledge that truly belongs together

Reuse a shared rule through clear inputs and results. Keep independent policies separate and check the effect of a shared change on its callers.

Connect this idea with sharing rules across real files and focused responsibilities and composition.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Version Control with Git →