Skip to content

Instantly share code, notes, and snippets.

@regularfry
Created July 23, 2014 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save regularfry/94bb45f63e15e60d4655 to your computer and use it in GitHub Desktop.
Save regularfry/94bb45f63e15e60d4655 to your computer and use it in GitHub Desktop.

SRP: two approaches, two definitions of “responsibility”

Using “Uncle Bob” and “Sandi Metz” as figureheads of each style because they’ve both written about them, not necessarily to imply any ownership of the concepts.

  • Uncle Bob: “Each module has a single responsibility TO SOMEONE”
    • If this applies at the class level, it can’t be usefully used at the method level
  • Sandi Metz: “Each class/method has a single responsibility FOR SOMETHING”
    • This implies the same test applied at two different levels of abstraction because the class’s responsibility is composed of the responsibilities of the methods

Direct tests to see if each is violated

Uncle Bob:

  • How many people could be fired because of a mistake specifying this module? If more than one person, you have a violation.

Sandi Metz

  • Personification: “Mr Gear, what is your ratio?” If the question doesn’t make sense, you might have an SRP violation
    • OR you might just have a misnamed class, this is not a clear test. Should your first instinct be to reach for a renaming, or Extract Method?
  • Description: “What this class does is xxx AND yyy” <- “and” implies violation
    • Admits weasel words. “This class represents a user” means very little. We need more defined rules to construct the description.

Not finding a violation via either of these tests doesn’t mean you have a class with a single responsibility. They admit false negatives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment