DRY is all about code factorization
This article is the first in a series dedicated to good object-oriented design practices. It addresses the DRY principle, which translates to “Don’t Repeat Yourself”. Even though the examples provided here are written in Java, the DRY principle is common to all programming languages.
Quick definition
As from the book “The pragmatic programmer” :
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
In other words, each business logic or algorithm must exist uniquely within an application.
Violations of DRY
In the following sections, we will examine some violations of this principle.
Business logic duplication
In the code snippet below, the developer has duplicated the business logic of displaying an array of items.
Don’ts
//Code with duplication
public void someMethodWithoutDRY1(String[] arr) {
// Perform some business treatments.....
// Display an array of strings on the screen
for (String stringValue : arr) {
System.out.println(stringValue + " ");
}
// Perform other business treatments...
}
public void someMethodWithoutDRY2(Integer[] arr) {
// Perform some business treatments.....
// Display an array of integers on the screen
for (Integer intValue : arr) {
System.out.println(intValue + " ");
}
// Perform other business treatments...
}
Do’s
Here is the code after proper application of the DRY principle
//Without duplicated code
public void displayWithDRY(Object[] arr) {
// Display an array of objects on the screen
for (Object objectValue : arr) {
System.out.println(objectValue + " ");
}
}
public void someMethodWithDRY1(String[] arr) {
// Perform some business treatments.....
// Display an array of strings on the screen
displayWithDRY(arr);
// Perform other business treatments....
}
public void someMethodWithDRY2(Integer[] arr) {
// Perform some business treatments.....
// Display an array of integers on the screen
displayWithDRY(arr);
// Perform other business treatments...
}
Algorithm duplication
Another common violation of the DRY principle is algorithm duplication.
In the code snippet below, the developer has duplicated the algorithm used to calculate the area of a rectangle.
Don’ts
class Rectangle {
double width;
double length;
Rectangle(double width, double length) {
this.length = length;
this.width = width;
}
public double calculateArea() {
return width * length;
}
}
class Square {
double side;
Square(double side) {
this.side = side;
}
public double calculateArea() {
return side * side;
}
}
//Testing
public static void main(String[] args) {
Rectangle r = new Rectangle(2, 3);
Square s = new Square(2);
System.out.println(r.calculateArea()); // displays 6
System.out.println(s.calculateArea()); // displays 4
}
Let’s see how to fix it.
Do’s
We change the Square class as follows:
class SquareDRY extends Rectangle {
double side;
SquareDRY(double side) {
super(side, side);//invokes the superclass constructor Rectangle(double width, double length)
}
//No need to rewrite the calculateArea() method as it's inherited from the superclass
}
//Testing
public static void main(String[] args) {
Rectangle r = new Rectangle(2, 3);
Rectangle s = new SquareDRY(2);
System.out.println(r.calculateArea()); // displays 6
System.out.println(s.calculateArea()); // displays 4
}
Conclusion
In this quick article, we examine two cases of violation of the DRY principle: business logic duplication and algorithm duplication. For each of these cases, we have seen what to do and what not to do.
What are your thoughts about the DRY principle?
Pingback: Inheritance in Java: Building on Basics for Powerful Programming