principles:principle_of_separate_understandability
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
principles:principle_of_separate_understandability [2013-02-12 11:37] – example 1 christian | principles:principle_of_separate_understandability [2013-06-22 20:44] – christian | ||
---|---|---|---|
Line 33: | Line 33: | ||
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 or the module has the wrong abstraction. So strategies for making a solution more compliant with PSU are: | ||
- | * Move the conflicting functionality to another module where it fits better (see [[Tell don't Ask/ | + | * Move the conflicting functionality to another module where it fits better (see [[Tell don't Ask/ |
* Build up a new module for the conflicting functionality (see [[High Cohesion|HC]]). | * Build up a new module for the conflicting functionality (see [[High Cohesion|HC]]). | ||
* Find the right abstraction for the module that allows the functionality to stay here (see [[Model Principle|MP]]). | * Find the right abstraction for the module that allows the functionality to stay here (see [[Model Principle|MP]]). | ||
+ | |||
+ | ===== Caveats ===== | ||
+ | |||
+ | See section [[#contrary principles]]. | ||
+ | |||
===== Origin ===== | ===== Origin ===== | ||
Line 65: | Line 70: | ||
* [[Information Hiding/ | * [[Information Hiding/ | ||
- | * [[Low Coupling]] (LC): One kind of couplings are logical couplings. These are especially hard to detect but should be avoided. PSU describes one aspect | + | * [[Model Principle]] (MP): The model contains the only information that should be necessary to understand the module. And if the abstraction |
- | * [[Model Principle]] (MP): The model contains the only information that should be necessary | + | * [[Tell, don't Ask/ |
+ | * [[Low Coupling]] (LC): Not adhering | ||
==== Principle Collections ==== | ==== Principle Collections ==== | ||
Line 73: | Line 79: | ||
- | ===== Example | + | ===== Examples |
==== Example 1: Parsing Data ==== | ==== Example 1: Parsing Data ==== | ||
Line 82: | Line 88: | ||
* '' | * '' | ||
- | In such a scenario it might be convenient to simplify '' | + | In such a scenario it might be convenient to simplify '' |
This is a simple solution (see [[Keep It Simple Stupid|KISS]]) but it violates PSU. '' | This is a simple solution (see [[Keep It Simple Stupid|KISS]]) but it violates PSU. '' | ||
Line 88: | Line 94: | ||
A better solution (wrt. PSU) would be to give '' | A better solution (wrt. PSU) would be to give '' | ||
+ | ==== Example 2: Dependent Private Methods ==== | ||
+ | |||
+ | In a module that computes results in a bowling game there might be a method '' | ||
+ | |||
+ | <code java> | ||
+ | private int ball; | ||
+ | private int[] itsThrows = new int[21]; | ||
+ | | ||
+ | private boolean strike() | ||
+ | { | ||
+ | if (itsThrows[ball] == 10) | ||
+ | { | ||
+ | ball++; | ||
+ | return true; | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Here the method not only computes if the current throw is a strike or not but also advances the counting variable '' | ||
+ | |||
+ | The following solution is better: | ||
+ | <code java> | ||
+ | private int rolls[] = new int[21]; | ||
+ | |||
+ | private boolean isStrike(int frameIndex) | ||
+ | { | ||
+ | return rolls[frameIndex] == 10; | ||
+ | } | ||
+ | </ | ||
+ | Here no counting variable is increased in some way. Furthermore this method does not rely on a correctly set private variable but gets a parameter. | ||
+ | |||
+ | This example is taken from Robert C. Martin. | ||
+ | * First version: see ((http:// | ||
+ | * Second version: see ((http:// | ||
===== Description Status ===== | ===== Description Status ===== | ||
/* Choose one of the following and comment out the rest: */ | /* Choose one of the following and comment out the rest: */ | ||
/ | / | ||
- | + | /*[[wiki: | |
- | [[wiki: | + | [[wiki: |
- | + | ||
- | /*[[wiki: | + | |
===== Further Reading ===== | ===== Further Reading ===== | ||
+ | ===== Discussion ===== | ||
+ | Discuss this wiki article and the principle on the corresponding [[talk: |
principles/principle_of_separate_understandability.txt · Last modified: 2021-10-18 22:13 by christian