DEV Community

Discussion on: Top 10 Object-Oriented Design Principles for writing Clean Code

Collapse
 
javinpaul profile image
javinpaul

Hello @Rasmas, you may be right but for the most part "duplicate code" has been evil in coding, at least that's what I have learned. DRY is a reminder to address that duplication, but it's not just code but also the functionality. Having the same functionality at two places makes it hard to maintain.

Collapse
 
mindplay profile image
Rasmus Schultz

Having the same functionality at two places makes it hard to maintain.

That depends.

You can have similar (even identical) code in two different places for different reasons - for example, new devs often conflate input validation with domain validation, because these are often similar in terms of code, but they're done for very different reasons.

Having similar (or identical) code at two places, in this case, makes the codebase easier to maintain, because you can change one without affecting the other.

Real-world example: validation of an address form on the checkout page of an e-commerce page - versus validation of an address form on an admin-only page on the back-end of the same system.

Even if these input validations are 90% similar today, there's a very real risk of one needing to change independently of the other. If there's a complex aspect to these validations, say, validating a street address via an API, that concern can be factored to a common service - but the validation itself (in this case electing to use said service or not) is probably simple and easy enough to maintain, so there's no good reason to mix these validation concerns; eliminating similar (even identical) code in this case creates a liability.

Conflating unrelated responsibilities, even if it eliminates similar/identical code, can make a project harder to maintain.

DRY is harmful dogma that teaches new developers to view complexity as a function of the number of lines of code - that the objective is always to have as few lines of code as possible. Nowhere does it say, "unless that makes the software harder to maintain".

Duplication isn't duplication unless it's duplicating the same responsibility.

DRY is harmful that way, and I really think we need to teach a more nuanced approach.