S For Solid: Understanding the Single Responsibility Principle

Chirag Vaswani
3 min readMay 14, 2024

--

Photo by Katie Smith on Unsplash

Each function or module should do one thing really well

The single responsibility principle, or SRP, is a rule in software design that means keeping things simple by having each part of your software do just one thing. Why?

Because when codebase is simpler, it’s easier to build, change, test, and understand.

Imagine you have a cluttered pantry with ingredients for cooking, cleaning supplies, and random tools all jumbled together. It’s chaotic and frustrating to find what you need! But if you organize things neatly, with spices in one section, canned goods on shelves, and utensils in drawers, it’s much tidier and efficient. That’s how SRP works!

Here’s why SRP is cool:

Easier Changes: When you want to make a change, it’s simpler if each part of your software only does one thing. Like if you want to update an app feature, you only have to focus on that specific part, not the whole app.

Less Confusion: Think of SRP as a rule that helps keep your software organized. If each part of your code has just one job, it’s like having each ingredient in a recipe separated into its own container in the kitchen. No more searching through a cluttered pantry to find what you need!

You can Focus on bigger tasks: Just like a well-organized pantry makes cooking easier, writing code that follows SRP makes updating and changing your software simpler. Each ingredient in the pantry has its own container, making it easy to find and use. Similarly, each part of your code should have a clear purpose, making it easier to understand and modify.

How to write code that’s easier to change?

Each function or module should do one thing well, whether it’s calculating a value, handling user input, or formatting data.

When writing code, think about breaking down tasks into smaller, focused functions or modules, just like separating ingredients in your pantry. Each function or module should do one thing well, whether it’s calculating a value, handling user input, or formatting data. This way, when you need to make a change or add a new feature, you only need to update or extend the specific part of the code related to that task, rather than wading through a tangled mess of intertwined responsibilities.

When each part of your code has a single responsibility, it’s easier to identify where changes need to be made when maintaining the system. You can quickly pinpoint which module or function is responsible for a particular task, making it easier to update or refactor that specific part without affecting the rest of the system.

Moreover, by keeping your codebase organized and focused, you free up mental bandwidth to tackle bigger-picture problems related to the design and architecture of your system. Instead of getting bogged down by tangled, spaghetti-like code, you can devote more time and energy to strategic planning, optimization, and ensuring that your system meets the evolving needs of your users.

In essence, SRP acts as a foundation for scalability and maintainability, allowing you to focus on solving higher-level problems and improving the overall design of your software system.

Real-World Examples:

Java Persistence API (JPA): JPA is like a specialized tool for managing data in Java programs. Its sole focus is on ensuring that data is stored and retrieved correctly from databases. It doesn’t try to be a jack-of-all-trades; instead, it excels at its one job. For tasks like password strength checking, you’d turn to a different tool.

UNIX Operating System Philosophy: UNIX follows the principle of making each program do one thing well. Instead of piling on new features to existing programs, UNIX encourages building new tools from scratch for different tasks. This philosophy resonates with the Single Responsibility Principle, emphasizing the importance of clarity and simplicity in software design.

So, next time you’re building something, ask yourself: “What’s the one job of this piece?” If you find yourself saying “and,” it might be time to simplify. Think of it like organizing your toys or making toast — keep it simple, one job at a time!

Thank you

--

--