The DRY Principle
Fast recall
Quick revision
Core answer
Whether you understand DRY as 'one source of truth for a concept' (not 'never repeat characters') and know when to extract.
Over-extracting (DRYing unrelated things that just look similar) or blind copy-paste (the WET opposite).
DRY = one source of truth per concept; extract a method/validation/constant/partial so a change lives in one place. Not 'never repeat characters' — over-extraction is a trap.
Interview-ready guide
Interview answer
DRY stands for Don't Repeat Yourself. Its useful meaning is that one business fact or decision should not be maintained in several places, because those copies can disagree after a change.
For example, if a discount rule appears in a controller, invoice model, and background job, changing the threshold requires three coordinated edits. Moving that rule to a named DiscountPolicy gives all callers one calculation and one testable source.
Duplication can be code, data, or knowledge. A partial can own repeated view markup, a constant can own a stable mapping, and a method can own one transformation. The extracted name should describe the shared idea rather than hide several unrelated cases behind flags.
Similar syntax is not always the same knowledge. Two formatters may look identical today but belong to different products and change independently. Combining them too early creates coupling and conditional complexity that is harder to remove than the small duplication was.
DRY works best after the common rule is clear: centralise changes that must remain consistent, keep accidental similarity separate, and prefer a simple duplication over a misleading abstraction.
The DRY Principle
Direct Answer
DRY means every important piece of knowledge should have one authoritative representation. In Ruby, repeated business rules, mappings, or calculations should be given one clear method, object, or data source so a change cannot leave copies inconsistent. DRY is not a command to eliminate every similar line: two pieces of code that only look alike but change for different reasons may be safer left separate until their shared concept is proven.
Deep Dive & Architecture
The risk in duplication is divergent change. If tax eligibility is written in three request handlers, one bug fix may update only two. The repeated lines are a symptom; the duplicated definition of eligibility is the actual problem.
A useful extraction starts by naming the rule and locating the object responsible for it. A calculation about an order may belong to an Order or policy object; repeated display markup may belong to a partial; repeated test setup may belong to a factory or helper. One universal helper file is rarely the right owner.
Timing matters. The first two examples provide evidence but may still differ in hidden ways. Waiting until the reasons for change are known can reveal the correct parameters—or reveal that no shared abstraction exists.
An abstraction has a maintenance cost: name, API, dependencies, and tests. It has earned that cost when callers share one meaning and future rule changes should reach all of them together.
Code Example
class DiscountPolicy
THRESHOLD = 100
RATE = 0.10
def discount_for(total)
total >= THRESHOLD ? total * RATE : 0
end
end
policy = DiscountPolicy.new
policy.discount_for(80) # => 0
policy.discount_for(120) # => 12.0
# Controllers, invoices, and jobs call the same rule
# instead of copying the threshold and calculation.
Common Mistakes & Pitfalls
Over-extracting (DRYing unrelated things that just look similar) or blind copy-paste (the WET opposite).
Check your understanding
Answer these without looking back at the article.
- What is the difference between duplicated code and duplicated knowledge?
- When can a DRY refactor create the wrong abstraction?
- Where should a repeated business rule live in a Rails application?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.