One Responsibility Rule

From Bertrand Meyer's “Object Oriented Software Construction”, there was the statement:

A class has a single responsibility: it does it all, does it well, and does it only.
Classes, interfaces, functions, etc. all become large and bloated when they're trying to do too many things.

When a function has too many responsibilities, it becomes buried deep in "Special Formatting", which has a "Code Smell".
To avoid bloat and confusion, and ensure that code is truly simple (not just quick to hack out) we have to practice "Code Normalization", which seems to be a variation on “Once And Only Once” and also “Do The Simplest Thing That Could Possibly Work”.
This is part of “Responsibility Driven Design”.

Special Formatting :- Sometimes in a method you wind up wanting special formatting. We take it as a sign, instead, that you should rewrite the method. It could refer to the situations where you would want to indent the statements to focus / highlight some important logic.
Code Smell :- A code smell is a hint that something has gone wrong somewhere in your code.
Code Normalization :- The attributes and operations of a class should depend on the responsibility, the whole responsibility, and nothing but the responsibility. If this can't be made to work, the class has lower cohesion, and might be a candidate for re factoring.
Once and Only Once :- One of the main goals (if not the main goal) when Re factoring code is that each and every declaration of behaviour should appear “Once And Only Once”.
Do The Simplest Thing That Could Possibly Work :- "All that is complex is not useful. All that is useful is simple." – Mikhail Kalashnikov
Responsibility Driven Design :- Design based on the principle that some class has to be responsible for each task that the system will carry out. The responsible class may collaborate with other classes to carry out its task. (It's probably a good idea if each responsibility occurs Once And Only Once.)

0 comments: