views:

489

answers:

8

The first pattern stands for this acronym is SRP. Here is a quote.

the single responsibility principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class.

That's is simple and clear till we start to code ) Suppose we have a class with well defined SRP. To serialize this class instances we need to add special atrributes to that class. So now the class have other responsibility. Dosen't it violate SRP? Let's see other story. Interface implementation. Then we implement an interface we simply add other responsibility say dispose its resorces or compare its instances or whatever. So my question. Is it possible to keep SRP complete? How can we do it?

+8  A: 

I don't think that being serializable or disposable amounts to multiple responsibilities.

Steven Sudit
Agreed. And SRP is the most "solid" of the SOLID principles, IMO. The others may bend occasionally, but SRP should never be violated.
Stephen Cleary
@Stephen: I think the trick here is to understand what constitutes a single responsibility. In practice, this is going to vary, depending on what is expected of the class, as well as what is required for it to "play nice".
Steven Sudit
+1 -- adding an attribute isn't adding a responsibility
STW
Depends on what this attribute is doing. It may well add a lot of responsibilities.
Developer Art
well I don't think so. Interfaces or attributes are simply the tools. They extend the behaviour of a class.
Arseny
@Developer Art: I would say that adding an interface or attribute *does* change the class, but it doesn't *necessarily* change its *responsibility*. It all depends on what's added. IDisposable, for example, is a matter of GC plumbing, not a feature. On the other hand, if a User class suddenly implemented IEnumerable<Anteaters>, that would violate SRP (and common sense).
Steven Sudit
+22  A: 

As you will one day discover, none of the most known principles in software development can be 100% followed.

Programming is often about making compromises - abstract pureness vs. code size vs. speed vs.efficiency.

You just need to learn to find the right balance: not let your application fall into abyss of chaos but not tie yourself hands with multitude of abstraction layers.

Developer Art
Agreed that attempting to follow rules without understanding them is a big mistake. Rules have reasons, and when your context doesn't fit those reasons, you necessarily have an exception.
Steven Sudit
Agreed with you!
Arseny
+5  A: 

Well I suppose the first thing to note is that these are just good Software Engineering principles - you have to apply judgment also. So in that sense - no they are not solid (!)

I think the question you asked raises the key point - how do you define the single resposibility that the class should have?

It is important not to get too bogged down on details when defining a responsibility - just because a class does many things in code dosn't mean that it has many responibilities.

However, please do stick with it though. Although it is probably impossible to apply in all cases - it is still better than having a single "God Object" (Anti-Pattern) in your code.

If you are having problems with these I would recommend reading the following:

  • Refactoring - Martin Fowler: Although it is obviously about refactoring, this book is also very helpful in displaying how to decompose problems into their logical parts or resposibilities - which is key to SRP. This book also touches on the other principles - however it does it in a lot less academic way than you may have seen before.

  • Clean Code - Robert Martin: Who better to read than the greatest exponent of the SOLID principles. Seriously, I found this to be a really helpful book in all areas of software craftsmanship - not just the SOLID principles. Like Fowler's book, this book is pitched at all levels of experiance so I would recommend to anyone.

David Relihan
Clean Code was the book I was reading when I finally had the OO/Testable-code light pop on over my head. The best part of the book is where is cleans up code with a before/after sample. What really struck me was visually how much our code *looked* like the before, it really helped me understand what direction to move the code in for the sake of sanity
STW
+1  A: 

The thing to remember about design principles is there's always exceptions, and you wont always find that your scenario/implementation matches a given principle 100%.

That being said, adding attributes to the properties isn't really adding any functionality or behavior to the class, assuming your serialization/deserialization code is in some other class. You're just adding information about the structure of your class, so it doesn't seem like a violation of the principle.

wsanville
+1  A: 

I think there are many minor, common tasks a class can do without them obscuring the primary responsibility of a class: Serialisation, Logging, Exception Handling, Transaction Handling etc.

It's down to your judgement as to what tasks in your class constitute an actual responsibility in terms of your business/application logic, and what is just plumbing code.

JonoW
A: 

To better understand the SOLID principles you have to understand the problem that they solve:

Object-oriented programming grew out of structured/procedural programming--it added a new organizational system (classes, et al) as well as behaviors (polymorphism, inheritance, composition). This meant that OO was not seperate from structured/procedural, but was a progression, and that developers could do very procedural OO if they wanted.

So... SOLID came around as something of a litmus test to answer the question of "Am I really doing OO, or am I just using procedural objects?" The 5 principles, if followed, means that you are quite far to the OO side of the spectrum. Failing to meet these rules doesn't mean you're not doing OO, but it means its much more structural/procedural OO.

STW
Or, in other words, you're failing to take advantage of OO. This is bad, not neutral.
Steven Sudit
+1  A: 

By changing your definition of "single responsibility" - SOLID principles are quite liquid and (like other catchy acronyms of acronyms) don't mean what they seem to mean.

They can be used as a checklist or cheat sheet, but not as complete guidelines and certainly not learning material.

ima
"SOLID principles are quite liquid"I like it ))
Arseny
+3  A: 

There's a legitimate concern here, as these cross-cutting concerns (serialization, logging, data binding notification, etc.) end up adding implementation to multiple classes that is only there to support some other subsystem. This implementation has to be tested, so the class has definitely been burdened with additional responsibilities.

Aspect-Oriented Programming is one approach that attempts to resolve this issue. A good example in C# is serialization, for which there is a wide range of different attributes for different types of serialization. The idea here is that the class shouldn't implement code that performs serialization, but rather declare how it is to be serialized. Metadata is a very natural place to include details that are important for other subsystems, but not relevant to the testable implementation of a class.

Dan Bryant
That's interesting. I can tell other approach. Remember I read a book about DDD (Domain Driven Design) where author suggests to not write atttibutes but move serialization responsibility to other class. They name it repository.
Arseny
@Arseny: That's interesting, but I don't see how it would work well in a .NET environment.
Steven Sudit