SOLID Design Principles: Learn the Single-Responsibility Principle
Tags: solidprinciples, cleancode, programming, python
Published: 2023-11-19 11:06:07

👋 Hey there, fellow software enthusiasts! 
I'm Revisto, a passionate software engineer, and I want to dive into the exciting world of SOLID principles. 

**SOLID** is a set of rules and principles that can help us create maintainable, reusable, and flexible software designs. Essentially, it provides guidelines for building software that can help our projects grow easily.

So, what does SOLID stand for? Let's break it down:

- S: Single Responsibility Principle
- O: Open-Closed Principle
- L: Liskov Substitution Principle
- I: Interface Segregation Principle
- D: Dependency Inversion Principle


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fr7q0aeo4sbuo4g6a8zv.png)



In this article, we'll take a closer look at each of these principles, starting with the first one: the Single Responsibility Principle.

The Single Responsibility Principle (SRP) states that every **entity** in your software should have one and **only one responsibility**. In simpler terms, it means that a class or function should have a single job to do.

Robert C. Martin states:
> A class should have only one reason to change.


Think about it this way: if you have a class that's responsible for multiple tasks or purposes, you'll find yourself needing to modify the entire class whenever any of those tasks change. This can quickly become a nightmare, as it increases the risk of breaking other parts of the class or function unintentionally.

To adhere to the Single Responsibility Principle, it's important to identify the distinct responsibilities within your software and **separate them into different classes or functions**. By doing so, you ensure that each entity has a clear purpose and is focused on a specific task.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/neyw0eqnq1esjy7e5jb7.jpg)



## 👨‍💻 Example:
Now, let's take a look at some Python examples to illustrate the Single Responsibility Principle in action:

```python
class Customer:
    def __init__(self, name, email):
        self.name = name
        self.email = email

    def send_email(self, message):
        # sending an email to the customer
        pass

    def place_order(self, order):
        # placing an order
        pass

    def generate_invoice(self, invoice):
        # generating an invoice
        pass

    def add_feedback(self, feedback):
        # add feedback
        pass
```

Here is a Customer class, it's responsible for sending emails to the customer, placing an order, generating invoices, and adding feedback. 
**Are all these functions related to the Customer class?**
❌ The short answer is NO.
The customer class must be responsible for tasks and jobs only related to the customer. `send_email` is another task related to the Notification system. `place_order`  should be handled by another class related to orders etc.

```python
class Customer:
    def __init__(self, name, email):
        self.name = name
        self.email = email

class EmailService:
    def send_email(self, customer, message):
        # Sending an email to the customer
        pass

class OrderService:
    def place_order(self, customer, order):
        # Placing an order
        pass

class InvoiceService:
    def generate_invoice(self, customer, invoice):
        # Generating an invoice
        pass

class FeedbackService:
    def add_feedback(self, customer, feedback):
        # add customer feedback
        pass
```

More examples at 
[my GitHub](https://github.com/Revisto/SOLID/tree/master/single-responsibility)

## What happens if we adhere to the single responsibility principle?

By adhering to the Single Responsibility Principle, we can achieve code that is easier to understand, maintain, and test. Changes in one responsibility won't affect other unrelated parts of the codebase, reducing the risk of introducing bugs and improving overall code quality.


In the next articles, we'll explore the other SOLID principles, so stay tuned for more exciting insights into building better software. 

Thanks for reading. Feel free to comment your thoughts😊. Hope this post was helpful. You can hit me up on [Linked In](https://www.linkedin.com/in/revisto/) and [Github](https://www.github.com/revisto/).

Happy coding!