principles:principle_of_separate_understandability
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
principles:principle_of_separate_understandability [2013-10-07 12:17] – christian | principles:principle_of_separate_understandability [2021-10-18 22:13] (current) – +++ restored +++ christian | ||
---|---|---|---|
Line 11: | Line 11: | ||
===== Principle Statement ===== | ===== Principle Statement ===== | ||
- | Each module shall be understandable on its own -- without knowing anything about other modules. | + | Each module shall be understandable on its own---without knowing anything about other modules. |
Line 20: | Line 20: | ||
* By looking at the public methods of a class it should be clear why they are there. That means there should be no method that is only there because a specific other module needs it. | * By looking at the public methods of a class it should be clear why they are there. That means there should be no method that is only there because a specific other module needs it. | ||
* By looking at the implementation of a module it should be clear how it works and why it was done that way. That means there should be no code that is solely there in order to make another module work. | * By looking at the implementation of a module it should be clear how it works and why it was done that way. That means there should be no code that is solely there in order to make another module work. | ||
- | * By looking at a private method it should be clear what it does. That means there should be no (private) method that is only meaningful in the context of another method. | + | * By looking at a private method it should be clear what it does. That means there should be no (private) method that is only meaningful in the context of another method |
- | * By looking at a method invocation it should be clear what happens, why the parameters are there, and what they specify. It should not be necessary to look up the method implementation. | + | * By looking at a method invocation it should be clear what happens, why the parameters are there, and what they specify. It should not be necessary to look up the method implementation |
* By looking at a single line of code it should be clear what it does without having to look up other code. | * By looking at a single line of code it should be clear what it does without having to look up other code. | ||
Line 32: | Line 32: | ||
* You have to find the implementation and jump there (modern IDEs help here but it takes time nevertheless) | * You have to find the implementation and jump there (modern IDEs help here but it takes time nevertheless) | ||
* While doing so, you have to memorize the call and the context of the call. If implementation and call are not colocated (which is preferable but not always possible) you won't see the call anymore so you have to memorize it. | * While doing so, you have to memorize the call and the context of the call. If implementation and call are not colocated (which is preferable but not always possible) you won't see the call anymore so you have to memorize it. | ||
- | * Then you have to read the code and mentally inline it. | + | * Then you have to read the code and [[glossary: |
- | * If you could not memoroze | + | * If you could not memorize |
* After you did all that you have to jump back and continue reading the method with the call you just mentally inlined. | * After you did all that you have to jump back and continue reading the method with the call you just mentally inlined. | ||
Line 42: | Line 42: | ||
===== Strategies ===== | ===== Strategies ===== | ||
- | When a module does not comply with PSU, this means that either a part of the functionality of the module does not belong here or the module has the wrong abstraction. So strategies for making a solution more compliant with PSU are: | + | When a module does not comply with PSU, this means that either a part of the functionality of the module does not belong here (see [[#Example 1: Parsing Data|example 1]]) or the module has the wrong abstraction |
* Move the conflicting functionality to another module where it fits better: [[refactorings: | * Move the conflicting functionality to another module where it fits better: [[refactorings: | ||
Line 85: | Line 85: | ||
* [[Tell, don't Ask/ | * [[Tell, don't Ask/ | ||
* [[Low Coupling]] (LC): Not adhering to PSU means that responsibilities are scattered across several modules. This typically also means increased coupling. | * [[Low Coupling]] (LC): Not adhering to PSU means that responsibilities are scattered across several modules. This typically also means increased coupling. | ||
+ | * [[Single Level of Abstraction]] (SLA): The purpose of PSU is to avoid [[glossary: | ||
==== Principle Collections ==== | ==== Principle Collections ==== | ||
Line 141: | Line 142: | ||
* First version: see ((http:// | * First version: see ((http:// | ||
* Second version: see ((http:// | * Second version: see ((http:// | ||
+ | |||
==== Example 3: Unnecessary State and Wrong Abstractions ==== | ==== Example 3: Unnecessary State and Wrong Abstractions ==== | ||
- | Have a look at the following piece of code: | + | This example is also inspired by Robert C. Martin. |
<code java> | <code java> | ||
public String make(char candidate, int count) | public String make(char candidate, int count) | ||
Line 152: | Line 154: | ||
} | } | ||
</ | </ | ||
- | What does it do? Certainly some information is missing. This piece of code is not separately understandable. You might feel the urge to ask for the implementation of '' | + | What does it do? Certainly some information is missing |
<code java> | <code java> | ||
Line 237: | Line 239: | ||
</ | </ | ||
- | Only if you read all that code, you really get what's going on. Also if you started with some other method, you would not understand it. It's clear what '' | + | Only if you read all that code, you really get what's going on. Also if you started with some other method, you would not understand it. It's clear what '' |
- | The functionality is buried in the class which is most obvious with the '' | + | The problem cannot be solved by moving or renaming methods or fields. The abstraction of the methods is wrong. The methods are just groupings of code and have no distinct meaning. The uncommon naming scheme of the methods lacking an imperative form of a verb might be an indicator for that. |
+ | |||
+ | The functionality is buried in the class which is most obvious with the '' | ||
A better solution might be the following: | A better solution might be the following: | ||
Line 294: | Line 298: | ||
===== Further Reading ===== | ===== Further Reading ===== | ||
+ | |||
+ | * [[http:// | ||
===== Discussion ===== | ===== Discussion ===== | ||
Discuss this wiki article and the principle on the corresponding [[talk: | Discuss this wiki article and the principle on the corresponding [[talk: | ||
+ |
principles/principle_of_separate_understandability.1381141055.txt.gz · Last modified: 2013-10-07 12:17 by christian