avatarBirat Rai

Summary

The web content discusses the DRY (Don't Repeat Yourself) principle as the 30th step in a series towards programming enlightenment, emphasizing the importance of having a single, unambiguous representation of knowledge within a system to avoid duplication and its associated maintenance issues.

Abstract

The article titled "Step 30: Don’t Repeat Yourself" by Steve Smith is part of a larger series aimed at achieving programming enlightenment. It introduces the DRY principle, a concept from "The Pragmatic Programmer," which advocates for a single, clear, and authoritative representation of knowledge within a system. The principle is crucial for reducing repetition and redundancy in software patterns and data, which in turn simplifies maintenance and minimizes the potential for bugs. The article explains that duplication increases the risk of bugs and complicates the codebase, making it harder for developers to understand and manage. To adhere to the DRY principle, developers are encouraged to avoid copy-and-paste coding, utilize design patterns like Factory and Strategy, and employ tools such as IDEs and static analysis tools to detect and eliminate duplicate code. The article also provides references and links to further reading on the topic.

Opinions

  • The DRY principle is essential for maintaining clean, bug-free, and understandable code.
  • Duplication in code leads to increased maintenance efforts and potential for bugs.
  • Adhering to the DRY principle supports the SOLID principle, which is another fundamental concept in software development.
  • Tools and design patterns are available to help developers avoid code duplication and follow the DRY principle effectively.
  • Understanding the DRY principle is part of a broader journey of learning and improvement for programmers.

Step 30: Don’t Repeat Yourself ~ Steve Smith

This is the 30th Step towards gaining the Programming Enlightenment series. If you didn’t learn the 29th Step, read it.

What is Don’t Repeat Yourself (D.R.Y)?

The DRY principle is stated as “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system” as stated in The Pragmatic Programmer.

DRY principle is aimed at reducing repetition of software patterns, replacing them with abstractions; and several copies of the same data, using data normalization to avoid redundancy.

Why don’t duplicate?

Every duplicate line into application needs to be maintained. If a potential source of bugs appear, it would have to be fixed in all of those duplicates.

It bloats the codebase making it much more difficult for developers to fully understand the entire system.

SOLID principle relies on DRY principle.

How to remove duplication?

  • Copy-and-paste coding usually litters the repetition, we can use different design pattern to avoid duplication like Factory Pattern, Strategy Pattern etc.
  • IDE are powerful to detect duplicate codes and one can extract them to a common method.
  • Use static analysis tools like PMD to detect duplicate lines.

TL;DR Follow DRY principle don’t WET code.

Go to the series.

Go to 29th Step

Go to the 31st Step.

References:

Programming
Coding
Software Development
Java
Android
Recommended from ReadMedium