|
|
Line 1: |
Line 1: |
− | == Bad Smell Number 1 - Duplicated Code ==
| + | [[Code smell 1 - Duplicate Code]] |
− | | + | |
− | Duplicated code is the smelliest of the code smells. The book "Refactoring" by Martin Fowler states that: "If you see the same code structure in more than one place, you can be sure that your program will better if you find a way to unify them." "Refactoring" states that the simplest duplicated code problem is when you have the same expression in two methods of the same class. The best way to fix this bad code smell is to use the Extract Method as shown below.
| + | |
− | | + | |
− | printOwing() {
| + | |
− | printBanner();
| + | |
− |
| + | |
− | //print details
| + | |
− | System.out.println ("name: " + _name);
| + | |
− | System.out.println ("amount " + getOutstanding());
| + | |
− | }
| + | |
− | | + | |
− | becomes
| + | |
− | | + | |
− | void printOwing() {
| + | |
− | printBanner();
| + | |
− | printDetails(getOutstanding());
| + | |
− | }
| + | |
− | | + | |
− | void printDetails (double outstanding) {
| + | |
− | System.out.println ("name: " + _name);
| + | |
− | System.out.println ("amount " + outstanding);
| + | |
− | }
| + | |
| | | |
| == See also == | | == See also == |
| * [[Don't repeat yourself]] | | * [[Don't repeat yourself]] |