principles:principle_of_separate_understandability
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Next revisionBoth sides next revision | ||
principles:principle_of_separate_understandability [2020-10-12 16:33] – old revision restored (2013-10-07 12:17) 159.69.186.191 | principles:principle_of_separate_understandability [2020-10-12 16:33] – old revision restored (2013-10-07 11:46) 159.69.186.191 | ||
---|---|---|---|
Line 141: | Line 141: | ||
* First version: see ((http:// | * First version: see ((http:// | ||
* Second version: see ((http:// | * Second version: see ((http:// | ||
- | |||
- | ==== Example 3: Unnecessary State and Wrong Abstractions ==== | ||
- | |||
- | Have a look at the following piece of code: | ||
- | <code java> | ||
- | public String make(char candidate, int count) | ||
- | { | ||
- | createPluralDependentMessageParts(count); | ||
- | return String.format(" | ||
- | } | ||
- | </ | ||
- | 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 '' | ||
- | |||
- | <code java> | ||
- | private void createPluralDependentMessageParts(int count) | ||
- | { | ||
- | if (count == 0) | ||
- | { | ||
- | thereAreNoLetters(); | ||
- | } | ||
- | else if (count == 1) | ||
- | { | ||
- | thereIsOneLetter(); | ||
- | } | ||
- | else | ||
- | { | ||
- | thereAreManyLetters(count); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Again, you most likely won't be satisfied and ask for the rest of the implementation: | ||
- | |||
- | <code java> | ||
- | public class Statistics2 | ||
- | { | ||
- | | ||
- | { | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | |||
- | | ||
- | { | ||
- | | ||
- | | ||
- | | ||
- | |||
- | | ||
- | { | ||
- | | ||
- | | ||
- | } | ||
- | |||
- | | ||
- | { | ||
- | if (count == 0) | ||
- | { | ||
- | | ||
- | } | ||
- | else if (count == 1) | ||
- | { | ||
- | | ||
- | } | ||
- | else | ||
- | { | ||
- | | ||
- | } | ||
- | } | ||
- | |||
- | | ||
- | { | ||
- | | ||
- | verb = " | ||
- | | ||
- | } | ||
- | |||
- | | ||
- | { | ||
- | | ||
- | verb = " | ||
- | | ||
- | } | ||
- | |||
- | | ||
- | { | ||
- | | ||
- | verb = " | ||
- | | ||
- | } | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | 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 '' | ||
- | |||
- | A better solution might be the following: | ||
- | |||
- | <code java> | ||
- | public class Statistics3 | ||
- | { | ||
- | enum Number {SINGULAR, PLURAL} | ||
- | |||
- | | ||
- | { | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | |||
- | | ||
- | { | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | |||
- | | ||
- | { | ||
- | | ||
- | } | ||
- | |||
- | | ||
- | { | ||
- | | ||
- | } | ||
- | |||
- | | ||
- | { | ||
- | | ||
- | } | ||
- | |||
- | | ||
- | { | ||
- | | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Here virtually every piece of code is understandable on its own. | ||
===== Description Status ===== | ===== Description Status ===== |
principles/principle_of_separate_understandability.txt · Last modified: 2021-10-18 22:13 by christian