Skip to content

Single Responsibility Principle [SRP]

A class should only have one reason to change.

Types of responsibilities:

  • persistence
  • logging
  • validation
  • business logic (broad category, can be broken down further)

Responsibilities change at different times for different reasons.

Separation of Concerns

Programs should be separated into discreet sections, each one addressing a separate concern or set of information that affects the program.

Cohesion

Class elements that belong together are cohesive

class-cohesion-coupling

SRP and Testing

It is easier to test one responsibility in isolation.

If classes have lots of responsibilities, tests become:

  • longer
  • more complex
  • brittle
  • coupled with implementation