The Single Responsibility Principle
blog.8thlight.com
The Single Responsibility Principle
1–10 of 22 posts
Re: The Single Responsibility Principle
#2Thank you.
Re: The Single Responsibility Principle
#3But often this gets conflated in OO because objects are the hearth of modularization (via encapsulation) along with state and interaction and any number of other things.
---
As a comparison point, you might examine ML modules. They look a bit like this
module counter(X)
count : X -> Int
incr : X -> X
and they specify nothing more than the fact that some unknown type X satisfies the interface `(count, incr)`. We can then create a concrete implementation of such a counter incCounter : counter
incCounter = structure(Int)
count n = n
incr n = n + 1
The incCounter internally uses `Int` to represent `X`, but externally it's completely impossible to tell. This means that modules define exactly two things: encapsulation and interface.---
So why does this fall down in OO? Because objects lend themselves to being thought of as entities which move through time and space in a stateful fashion. This means you're also likely to encapsulate differences of entity without regard for how they might change together or apart.
Returning to Parnas' quote: it's a bad idea to decompose into modules based on a flowchart. Flowcharts allow you to emphasize the entities of your system, but they are not demonstrating the boundaries of change.
So you can probably get better OO design by being clear when you're using objects as entities (and thus perhaps you do not need encapsulation at all!) and when you're using them as modules. Once this distinction is made it can be clear when objects will derive from flowcharts and when objects will derive from selections of choices made by people.
Re: The Single Responsibility Principle
#4Re: The Single Responsibility Principle
#5This is too vague to be a "principle", and only causes confusion. It is about as precise, and as useful, as the "write good code" principle.
Re: The Single Responsibility Principle
#6"organizations which design systems ... are constrained to produce designs which are copies of the communication structures of these organizations" - M. Conway
More on http://c2.com/cgi/wiki?ConwaysLaw and http://en.wikipedia.org/wiki/Conway's_law
However, I also hate this mental model of software engineering because I have often found it easier to refactor the organization. Maybe because I'm a prima donna and only like working at startups.
I think it's better to require each portion of code has a narrow interface so you can reason in your mind easily about what that code segment does and should do into the future. A function or a class must promise what it should deliver given some inputs and not violate that expectation. If you ever had to reason about code using invariants, you'll grok this.
Re: The Single Responsibility Principle
#7While he might understand what he is talking about, based on a corpus of experience, people who learn about this "principle" as they learn to program do not typically understand it and tend to draw only the wrong conclusions from it. What makes for coupling, and how to avoid it, you simply learn with a lot of practice, and there are not many clearcut guidelines you can formulate, it all varies depending on the proble…
I consider myself fortunate to have written enough C++ to learn coupling and cohesion. When you screw these up in C++, you pay for it, over and over, through increased compile times and link times.
Re: The Single Responsibility Principle
#8"The Single Responsibility Principle (SRP) states that each software module should have one and only one reason to change. This sounds good, and seems to align with Parnas' formulation. However it begs the question: What defines a reason to change?" (emphasis added)
Ok I realize that languages evolve and all; and I see how the nearly universal appeal of using the phrase "begging the question" in this way will ensure it will soon make its way into the dictionary; but I think people should at least know the original meaning of the phrase [1] and that, in some pedantic or predominantly academic circles today, it is considered as incorrect usage. That is all.
Re: The Single Responsibility Principle
#9While he might understand what he is talking about, based on a corpus of experience, people who learn about this "principle" as they learn to program do not typically understand it and tend to draw only the wrong conclusions from it. What makes for coupling, and how to avoid it, you simply learn with a lot of practice, and there are not many clearcut guidelines you can formulate, it all varies depending on the proble…
Given that data, its easy for a developer to extrapolate (more general) principles that may apply for their own concrete situations and problems, if any.
If you do the extrapolation of the principle yourself but hide the data that caused you to arrive to that principle, nobody will learn anything.
I have no idea why software architects do this. I guess because they're so used to abstracting all the details away in software, they start thinking it also applies to teaching/writing. It does not.
Re: The Single Responsibility Principle
#10While he might understand what he is talking about, based on a corpus of experience, people who learn about this "principle" as they learn to program do not typically understand it and tend to draw only the wrong conclusions from it. What makes for coupling, and how to avoid it, you simply learn with a lot of practice, and there are not many clearcut guidelines you can formulate, it all varies depending on the proble…
A lot of people seem to chafe at these principles because they convey truths about a intangible reality they may not even be able to perceive. They only see more work for little payoff. They don't have the body of knowledge and experience (nor the intuition) to know when when to abstract more. We're also in a post-enterprise era, where any sort of deliberate design is often mocked by way of strawman AbstractComposerF…
Its no wonder people either tend to discard them or to apply them incorrectly.