Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Understanding the DRY principle: Don't Repeat Yourself

March 10, 2023

Understanding the DRY Principle: Don’t Repeat Yourself

As programmers, we all strive to write clean, efficient, and maintainable code. One of the key principles that helps us achieve this is the DRY principle, which stands for “Don’t Repeat Yourself”. This principle emphasizes the importance of reusability, maintainability, and reducing redundancy within our codebase.

In this blog post, we will explore the DRY principle in depth, provide extensive examples in C and Python, and explain the concepts in detail.

What is the DRY Principle?

The DRY principle is a software development principle aimed at reducing repetition and promoting code reusability. It suggests that every piece of knowledge or logic within a system should have a single, unambiguous representation. In other words, we should strive to write code in a way that avoids duplicating information, data, or logic.

By adhering to the DRY principle, we can achieve several benefits, including:

DRY Principle in Practice: Examples in C and Python

Example in C

Let’s consider a simple example in C to illustrate the DRY principle. Suppose we have a program that calculates the area of a circle and the area of a rectangle. Initially, we might write two separate functions to calculate the area of each shape:

1
2
3
4
5
6
7
8
9
#include <stdio.h>

float calculateCircleArea(float radius) {
    return 3.14159 * radius * radius;
}

float calculateRectangleArea(float length, float width) {
    return length * width;
}

While these functions calculate the areas correctly, there is a degree of repetition in the logic. We can refactor the code to adhere to the DRY principle by creating a single function that accepts shape-specific parameters:

1
2
3
4
5
6
7
8
9
#include <stdio.h>

float calculateArea(char shape, float param1, float param2) {
    if (shape == 'circle') {
        return 3.14159 * param1 * param1;
    } else if (shape == 'rectangle') {
        return param1 * param2;
    }
}

By consolidating the logic into a single function, we eliminate redundancy and promote reusability.

Example in Python

In Python, we can apply the DRY principle to object-oriented programming as well. Consider a scenario where we have multiple classes representing different shapes, each with a method to calculate its area:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Circle:
    def __init__(self, radius):
        self.radius = radius

    def calculate_area(self):
        return 3.14159 * self.radius * self.radius

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def calculate_area(self):
        return self.length * self.width

In this example, the calculate_area method is repeated in each class. To adhere to the DRY principle, we can define a base class with a generic calculate_area method and have the specific shapes inherit from it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Shape:
    def calculate_area(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def calculate_area(self):
        return 3.14159 * self.radius * self.radius

class Rectangle(Shape):
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def calculate_area(self):
        return self.length * self.width

By introducing a base class and leveraging inheritance, we eliminate duplication of the calculate_area method and adhere to the DRY principle.

Conclusion

In this blog post, we’ve explored the DRY principle in depth, providing extensive examples in both C and Python. By understanding and applying the DRY principle, we can write code that is more maintainable, reusable, and easier to understand.

As programmers, it’s essential to continuously strive for DRY code, as it leads to more efficient development and ultimately results in higher-quality software. Remember, repetition is not always a virtue in programming - embrace the DRY principle and strive to keep your code clean and DRY!

Happy coding!


➡️ Understanding Data Sizes in C: Insights for the Embedded Programme


⬅️ Welcome to my blog: A Journey Through Code and Electronics


Go back to Posts.