Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Single Responsibility Principle a rule of OOP? [closed]

Tags:

An answer to a Stack Overflow question stated that a particular framework violated a plain and simple OOP rule: Single Responsibility Principle (SRP).

Is the Single Responsibility Principle really a rule of OOP?

My understanding of the definition of Object Orientated Programming is "a paradigm where objects and their behaviour are used to create software". This includes the following techniques: Encapsulation, Polymorphism & Inheritance.

Now don't get me wrong - I believe SRP to be the key to most good OO designs, but I feel there are cases where this principle can and should be broken (just like database normalization rules). I aggressively push the benefits of SRP, and the great majority of my code follows this principle.

But, is it a rule, and thus implies that it shouldn't be broken?

like image 864
Brad Leach Avatar asked Aug 19 '08 00:08

Brad Leach


People also ask

What are the 5 OOP principles?

The SOLID principles of OOP are: Single Responsibility Principle, Open-Closed Principle, Liskov Substitution Principle (LSP), Interface Segregation Principle (ISP), and Dependency Inversion Principle (DIP).

What is Open-Closed Principle in oops?

In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"; that is, such an entity can allow its behaviour to be extended without modifying its source code.

What is the principle behind the single responsibility principle?

The Single Responsibility Principle (SRP) The idea behind the SRP is that every class, module, or function in a program should have one responsibility/purpose in a program. As a commonly used definition, "every class should have only one reason to change". The class above violates the single responsibility principle.


2 Answers

Very few rules, if any, in software development are without exception. Some people think there are no place for goto but they're wrong.

As far as OOP goes, there isn't a single definition of object-orientedness so depending on who you ask you'll get a different set of hard and soft principles, patterns, and practices.

The classic idea of OOP is that messages are sent to otherwise opaque objects and the objects interpret the message with knowledge of their own innards and then perform a function of some sort.

SRP is a software engineering principle that can apply to the role of a class, or a function, or a module. It contributes to the cohesion of something so that it behaves well put together without unrelated bits hanging off of it or having multiple roles that intertwine and complicate things.

Even with just one responsibilty, that can still range from a single function to a group of loosely related functions that are part of a common theme. As long as you're avoiding jury-rigging an element to take the responsibilty of something it wasn't primarily designed for or doing some other ad-hoc thing that dilute the simplicity of an object, then violate whatever principle you want.

But I find that it's easier to get SRP correct then to do something more elaborate that is just as robust.

like image 140
Mark Cidade Avatar answered Sep 19 '22 08:09

Mark Cidade


None of these rules are laws. They are more guidelines and best practices. There are times when it doesn't make sense to follow "the rules" and you need to do what is best for your situation.

Don't be afraid to do what you think is right. You might actually come up with newer and better rules.

like image 44
bruceatk Avatar answered Sep 21 '22 08:09

bruceatk