tags:

views:

315

answers:

6

I'm not sure I agree with it, so I'd like to find the book or journal article behind this idea so that I can check that I understand exactly what they're saying and what context they mean it.

I think I understand the idea - I just want to know the source so I can check where the idea comes from.

why I'm asking:

The term "do one thing" is vague and could mean lots of things including "only have one method per class" (ridiculous)... I think it could mean a single responsibility (i.e. lots of methods.) It's also not particularly useful because you have to judge when a single responsibility becomes complicated enough to need refactoring out into several responsibilities with some kind of delegation...

+16  A: 

SOLID Principles from Bob Martin.

Single Responsibility Principle to be exact.

Although, in the first page of the chapter on the Single Responsibility Principle, he states:

This principle was described in the work of Tom DeMarco and Meilir Page-Jones. They called it cohesion.

The references for the work he mentioned are:

  • Structured Analysis and System Specification, Tom DeMarco, Yourdon Press Computing Series, 1979
  • The Practical Guide to Structured Systems Design, 2d. ed., Meilir Page- Jones, Yourdon Press Computing Series, 1988

Other sources (from S.Lott in comments) include:

Grundlefleck
Martin's list is a collection from previously published sources.
S.Lott
Also see http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design).
S.Lott
And see http://c2.com/cgi/wiki?AllocationOfResponsibility
S.Lott
Thanks that's great! I think that the Robert Martin SRP paper's use of "single reason for change" is slightly different from "objects of the class should only do one thing" which lots of people seem to repeat. I'm slightly concerned people might misinterpret that as a reason to build classes that are just a bunch of related "procedures" rather than an object!
cartoonfox
Good links, S. Lott. Included in answer :)
Grundlefleck
I've always interpreted 'do one thing' as 'have one responsibility'. What this means in practice is that an object should 'do' lots of things (i.e. have more than one method), but they will all be concerned with the behaviour and state associated to a single responsibility.
Grundlefleck
+1  A: 

Single responsibility principle - Check here for some information on the subject.

Otávio Décio
+1  A: 

What ever the source, I don't think it's a common idea in OO. An object might do many things.

Dani
I think it does matter where it comes from. I don't want the Chinese Whisper version - I want to know the well-researched, original intention behind what gets repeated so I can settle on an informed judgement just like people do in real disciplines ;-)
cartoonfox
+2  A: 

I'm pretty sure it's more of a function idea. Every object I've ever seen does more than one thing. Functions (methods) should only do one thing.

xenoterracide
+4  A: 

The man who invented the idea of modularity in software was Dr David Parnas. The classic paper is On the Criteria To Be Used in Decomposing Systems into Modules

While he doesn't talk about OO in general (as it wasn't around yet), the ideas of OO extend naturally from Dr Parnas' work. And part of this work is analysis on how to decompose your software into modules, and that modules should be single purpose.

Jay P.
Good answer - but not to the specific question I asked. ;-)
cartoonfox
Well, I believe Parnas himself said that OO is just a modern version of his concept of modules. So the one leads to the other :)
Jay P.
A: 

The Single Responsibility Principle (SRP) is quite common on the Java world. The references mentioned here are good.

The principle can apply pretty much to both classes and methods because it's a good idea for both.

The results of being aware of the SRP and applying it when possible are usually cleaner simpler code, but at the cost of more classes/methods. This can be of great value for reuse, testing and the next programmer to see it.

Derek Clarkson