8. The Single-Responsibility Principle (SRP)

image

© Jennifer M. Kohnke

None but Buddha himself must take the responsibility of giving out occult secrets . . .

—E. Cobham Brewer 1810–1897, Dictionary of Phrase and Fable (1898)

This principle was described in the work of Tom DeMarco1 and Meilir Page-Jones.2 They called it cohesion, which they defined as the functional relatedness of the elements of a module. In this chapter, we modify that meaning a bit and relate cohesion to the forces that cause a module, or a class, to change.

The Single-Responsibility Principle

A class should have only one reason to change.

Consider the bowling game from Chapter ...

Get Agile Principles, Patterns, and Practices in C# now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.