October 3, 2024 in Software Development Practices

Rethinking DRY: Beyond Code Duplication

Code examples illustrating the difference between code duplication and knowledge duplication in programming

In “The Pragmatic Programmer,” Andrew Hunt and David Thomas offer a fresh perspective on the DRY (Don’t Repeat Yourself) principle, challenging the conventional wisdom that it’s solely about eliminating code duplication. Instead, they emphasize that DRY is fundamentally about preventing the duplication of knowledge throughout the codebase.

So how we duplicate knowledge ?

Knowledge duplication occurs when the same logic or concept is repeated across different parts of the codebase, even if the code may not be identical. For example, methods like get_age and calculate_lifetime may appear distinct but duplicate the knowledge of age calculation.

def get_age(birthdate)
  Time.current - birthdate 
end 

def calculate_lifetime(birth_date)
  current_year = Time.now.year
  birth_year = birth_date.year
  current_year - birth_year - (birth_date.yday > Time.now.yday ? 1 : 0) 
end

Both methods deal with age calculation, representing a form of knowledge duplication. Changes to the logic would require updates in multiple places, risking inconsistency.

So knowledge duplication does not imply code duplication.

Does code duplication imply knowledge duplication ?

A common misconception is that code duplication always implies knowledge duplication. However, this is not necessarily the case. Let’s consider an example:

def validate_age(value): 
  validate_type(value, 'integer')
  validate_min_integer(value, 0)
end

def validate_quantity(value):
  validate_type(value, 'integer')
  validate_min_integer(value, 0)
end

Both validate_age and validate_quantity functions appear to have duplicated code, leading some to believe it violates DRY. However, as “The Pragmatic Programmer” points out:

“During code review, the resident know-it-all bounces this code, claiming it’s a DRY violation: both function bodies are the same. They are wrong. The code is the same, but the knowledge they represent is different. The two functions validate two separate things that just happen to have the same rules. That’s a coincidence, not a duplication.”

Conclusion

Redefining DRY urges us to balance eliminating redundancy with preserving clarity and context in our code. By embracing this nuanced understanding, we empower ourselves to write adaptable, resilient code that fosters knowledge sharing.



By browsing this website, you agree to our privacy policy.
I Agree