Duplicate code smell

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(Refactoring)
Line 1: Line 1:
 
Duplicated code is the smelliest of the code smells. The book ''Refactoring'' [[Martin Fowler 1999]] states that: "If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them." [[Martin Fowler 1999]] states that the simplest duplicated code problem is when you have the same expression in two methods of the same class.
 
Duplicated code is the smelliest of the code smells. The book ''Refactoring'' [[Martin Fowler 1999]] states that: "If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them." [[Martin Fowler 1999]] states that the simplest duplicated code problem is when you have the same expression in two methods of the same class.
  
== Refactoring ==
 
  
The best way to fix this bad code smell is to use [[Extract Method]] technique 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 ==

Revision as of 04:18, 11 September 2008

Duplicated code is the smelliest of the code smells. The book Refactoring Martin Fowler 1999 states that: "If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them." Martin Fowler 1999 states that the simplest duplicated code problem is when you have the same expression in two methods of the same class.


See also

Personal tools