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.
Find shared knowledge
Identify a rule that must stay consistent across callers.
Reuse appropriately
Parameterize the varying data while centralizing the rule.
Avoid false sharing
Keep independent school policies independent.
Two callers depend on one shared pass rule
Callers
Report and badge ask the same question.
Different student marksShared rule
One function defines the threshold.
mark >= 25Results
Each caller receives its own answer.
true or false
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: trueKeep 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
falseApply 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
35Recognize 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
Apply the idea to a new situation
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.