**SOLID PRINCIPLE OF SOFTWARE ENGINEERING**
Tags: softwaredevelopment
Published: 2024-05-26 18:37:19




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

Heyy? Do you know about the solid principle of software engineering.
Lemme show being more specific to react(give it a 10 mins read):

# S.O.L.I.D Principle

**General Description**:

A collection of design rules in software engineering called the S.O.L.I.D. principles is intended to make software more extensible, scalable, and maintainable. These guidelines aid programmers in handling the majority of software design issues and improve the readability, flexibility, and maintainability of their code. 

**More specific to React**:

By encouraging reusable, manageable, and modular code, the S.O.L.I.D. principles may be used in ReactJS to enhance the organization and caliber of React apps. Let's look at some real-world examples of how each of these ideas may be implemented in a ReactJS project:

### **Single Responsibility Principle (SRP):**

**Principle:** A component should have one reason to change, meaning it should have only one job.

**Example:** Instead of creating a component that handles user inputs, processes data, and also renders the results, it's better to split these responsibilities:


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

### **Open/Closed Principle (OCP):**

**Principle:** Components should be open for extension but closed for modification.

**Explanation**:

The Open/Closed Principle (OCP) is one of the key principles in object-oriented software design and is just as applicable in component-based architectures like ReactJS. OCP states that software entities (such as modules, classes, functions, or, in the case of React, components) should be open for extension but closed for modification. This means you should be able to change the behavior of a component without altering its source code.

**Understanding the Principle**

- **Closed for Modification**: Once a component is tested and deployed, you should not modify its internal code for new functionality to prevent the risk of breaking existing code. The original component should remain untouched, which ensures that updates or changes won't introduce new bugs in the existing functionalities.
- **Open for Extension**: You should be able to extend the existing components to add new behaviors. In React, this can be efficiently handled using props, higher-order components (HOCs), render props, or hooks to add new functionalities.

**Example:**

Higher-order components are a powerful pattern for reusing component logic. A higher-order component is a function that takes a component and returns a new component, thus extending the original component's functionality without directly modifying its implementation:


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



### **Liskov Substitution Principle (LSP)**

**Principle:** Derived components should be substitutable for their base components.

Explanation:

In React, LSP would imply that any component that extends another component should be able to replace its parent without any errors or changes in the expected behavior of the application. This principle is less commonly demonstrated in React because React favors composition over inheritance. However, understanding LSP is still beneficial, especially when designing component libraries or higher-order components where substitutability could become an issue.

Example:

Consider two components: **`BaseButton`** and **`PrimaryButton`**. The **`PrimaryButton`** component extends the **`BaseButton`** and should be able to replace **`BaseButton`** without any issues.

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




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


### **Interface Segregation Principle (ISP)**

**Principle:** Clients should not be forced to depend upon interfaces that they do not use.

**Explanation:**
In React, the Interface Segregation Principle implies that components should not be burdened with props or functionality that they don't need. Instead of creating large, monolithic components with many props, it's better to create smaller, more focused components with clear and specific purposes. This makes the components easier to understand, maintain, and reuse.

### **Example:**

Consider a component **`UserProfile`** that displays user information and allows editing:


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



If the **`UserProfile`** component is sometimes used only to display user information without the edit functionality, it violates ISP because it forces the client to depend on the **`onEdit`** prop even when it’s not needed.

To adhere to ISP, you can split this into two components:


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



### **Dependency Inversion Principle (DIP)**

**Principle:** High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g., interfaces). Abstractions should not depend on details. Details should depend on abstractions.

**Explanation:**
In React, DIP can be applied by ensuring that components depend on abstracted dependencies rather than concrete implementations. This can be achieved using hooks, context, or higher-order components (HOCs) to inject dependencies.

### **Example:**

Consider a component **`DataFetcher`** that fetches data from an API and displays it:


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



This component is tightly coupled to the fetch API and the URL, violating DIP. To adhere to DIP, we can abstract the data fetching logic and inject it into the component:

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