Richard Heathfield
3/7/2016 2:50:00 PM
On 07/03/16 04:25, Daniel Pitts wrote:
<snip>
> I'd even say that statements (in imperative languages) should also have
> one responsibility. This means statements such as "foo = ++bar;" are
> breaking the SRP, as the statement is responsible for incrementing bar
> *and* copying the result to foo.
>
> Maybe what I'm saying here is so self-evident that I shouldn't have
> brought it up, or maybe what I'm saying is just plain wrong, or even
> oversimplified. I'd like feedback.
I think the SRP is just plain wrong. Please bear with me while I paint a
picture. (A picture is not proof! But it can illustrate a truth, if
we're careful.)
I hope we can all agree that putting the entire program into a single
function is a mistake, for all but the very simplest of programs. Let's
say that we have a million things to do, and that each thing takes one
line. The function would be a million lines long, which is ludicrous. We
can think of this as being a very tall, very narrow program.
Okay, so let's go the other way. Let's have a million functions instead.
We now have a very short, very wide program. And it's just as ridiculous.
But we can make a lot more sense of the program if it has, say, a
thousand functions, each a thousand lines long. Now the program is still
quite tall, but not unmanageably so. And it's still quite wide, but not
unmanageably so.
That's my picture. Now for my argument. So you can stop bearing with me
now. :-)
If we had a Single Responsibility /Strategy/, I could live with that.
Let's try to make each function do one small job and do it well, but
let's accept that there will be times when it makes more sense for the
function to do a bigger job, because it makes some /other/ job simpler.
But a Single Responsibility /Principle/ brooks no argument. We cannot
say, with Groucho Marx, "these are my principles, and if you don't like
them... well, I have others". We must stick to our principles, right?
And if we don't stick to them, our code will fail the review. And so we
have /no choice/ but to go with the very short, very wide program. Every
function will be a one-liner. Every function must take only one
parameter. Every function must return only one value. Banish the IF!
It's ridiculous. It leads sensible, thoughtful people to wonder whether
we should turn
foo = ++bar;
into
foo = bar + 1; ++bar;
It will turn short functions into long functions, and then it will
insist that those long functions be turned into two or more shorter
functions, and we end up with a million one-line functions.
NO, sir! I will cheerfully accept that a Single Responsibility Strategy
can help to make maintenance easier when it is applied as /one/ of a
number of tools for writing better software. But I cannot accept it as a
principle.
These, ladies and gentlemen, are my strategies, and if /I/ don't want to
go where they lead, I have /other/ strategies.
It isn't a great one-liner, is it? But that's kind of the point. I'd
rather have a thousand great one-thousand-liners than a million great
one-liners.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within