Designing Classes with a Single Responsibility
I. DECIDING WHAT BELONGS IN A CLASS
You have an application in mind. You know what it should do. You may even have thought about how to implement the most interesting bits of behavior. The problem is not one of technical knowledge but of organization; you know how to write the code but not where to put it.
I 2.0 Grouping Methods into Classes
In a class-based OO language like Ruby, the categories you produce can affect how you think that regarding to your application. They outline a virtual world, one that constrains the imagination of everyone. You are constructing a box that will be tough to think outside of. Despite the importance of properly grouping into categories, at this early stage of your project, you can’t presumably latch on right away. If your application succeeds, many of the decisions you make today will need to be changed later. When that day comes, your ability to successfully make those changes will be determined by your application’s design.
I 2.1 Organizing Code to Allow for Easy Changes
Asserting that code ought to be simple to alter is like stating that kids ought to be polite; the statement is impossible to disagree with, yet it in no way helps a parent raise an agreeable child. You need concrete definitions of easiness and specific criteria by which to judge code. Existing code is easy to reuse, and the code you write ought to have the subsequent qualities. Code should be Transparent. The consequences of modification ought to be obvious within the dynamic code and in distant code that relies upon it. The cost of any modification ought to be proportional to the advantages the modification achieves. Usable Existing code should be usable in new and unexpected contexts. Exemplary code itself ought to encourage people who modify it to preserve these qualities. Code that is Transparent, Reasonable, Usable, and Exemplary (TRUE) can meet the needs of the future as well as the needs of the present. The first step in creating TRUE code is to make sure that every category contains a single, well-defined responsibility.