Code smells

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(Links)
 
(24 intermediate revisions by 9 users not shown)
Line 1: Line 1:
== Bad Smell Number 1 - Duplicated Code ==
+
"If it stinks, change it." --Kent Beck's grandma, discussing child-rearing philosophy.
  
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.
+
The term ''code smells'' was invented by [[Kent Beck]]. Chapter 3 of [[Martin Fowler 1999]] (co-authored by Beck) provides a good intro.
  
        printOwing() {
+
== Code smells motivate [[Refactoring]]. ==
                printBanner();
+
       
+
                //print details
+
                System.out.println ("name: " + _name);
+
                System.out.println ("amount " + getOutstanding());
+
        }
+
  
becomes
+
Beck needed a word that gave developers a license to change code because they ''felt'' like it; because they sensed something was ''aesthetically'' wrong with the code.  He didn't want something that suggested precision or direct quantifiablilty:
  
        void printOwing() {
+
:''One thing we won't try to do here is give you precise criteria for when a [[Refactoring]] is overdue.  What we will do is give you indications that there is trouble that can be solved by refactoring.  You will have to develop your own sense of how many instance variables is too manyinstance variables and how many lines of code in a method are too many lines. -- [[Martin Fowler 1999]], p75.''
                printBanner();
+
                printDetails(getOutstanding());
+
        }
+
  
        void printDetails (double outstanding) {
+
Ultimately, [[Christopher Alexander]] is lurking beneath this idea:
                System.out.println ("name: " + _name);
+
:''As far as I am concerned, patterns and XP are "the first try" and "the second try" of [[Kent Beck]] to put [[Christopher Alexander]]'s ideas into practice. Both of them are derivatives of the Alexanderian philosophy.''
                System.out.println ("amount " + outstanding);
+
        }
+
  
== See also ==
+
:''On Kent's second try, he very carefully avoided mentioning Alexander. Perhaps he thought that Alexander just confused people, and that it was better to avoid ideas like the QWAN. So, he invented a whole bunch of new vocabulary to convey these ideas, things like "Code smells" and [[Do the simplest thing that could possibly work]].''  -- [[Ralph Johnson]]
* [[Don't repeat yourself]]
+
 
 +
== Catalogue ==
 +
A alphabetical collection of code smells:
 +
 
 +
* [[Alternative classes with different interfaces smell]]
 +
* [[Comments smell]]
 +
* [[Data class smell]]
 +
* [[Data clumps smell]]
 +
* [[Divergent change smell]]
 +
* [[Duplicate code smell]]
 +
* [[Feature envy smell]]
 +
* [[Inappropriate intimacy smell]]
 +
* [[Incomplete library class smell]]
 +
* [[Large class smell]]
 +
* [[Lazy class smell]]
 +
* [[Long method smell]]
 +
* [[Long parameter list smell]]
 +
* [[Message chain smell]]
 +
* [[Middle man smell]]
 +
* [[Parallel inheritance hierarchies smell]]
 +
* [[Primitive obsession smell]]
 +
* [[Refused bequest smell]]
 +
* [[Shotgun surgery smell]]
 +
* [[Speculative generality smell]]
 +
* [[Switch statement smell]]
 +
* [[Temporary field smell]]
 +
 
 +
== Links ==
 +
 
 +
* [http://sis36.berkeley.edu/projects/streek/agile/bad-smells-in-code.html#Comments A short description] of some of these smells. (''Dead link'')
 +
* [http://www.soberit.hut.fi/sems/shared/deliverables_public/mmantyla_thesis_final.pdf A thesis on smells]
 +
* [http://www.soberit.hut.fi/mmantyla/BadCodeSmellsTaxonomy.htm Five smell categories]
 +
* [http://wiki.java.net/bin/view/People/SmellsToRefactorings Java.Net on smells]
 +
* [http://www.codinghorror.com/blog/archives/000589.html Coding Horror]
 +
 
 +
{{Template:CodeSmells}}
 +
 
 +
[[Category:Code smells]]

Latest revision as of 01:27, 13 January 2012

"If it stinks, change it." --Kent Beck's grandma, discussing child-rearing philosophy.

The term code smells was invented by Kent Beck. Chapter 3 of Martin Fowler 1999 (co-authored by Beck) provides a good intro.

Code smells motivate Refactoring.

Beck needed a word that gave developers a license to change code because they felt like it; because they sensed something was aesthetically wrong with the code. He didn't want something that suggested precision or direct quantifiablilty:

One thing we won't try to do here is give you precise criteria for when a Refactoring is overdue. What we will do is give you indications that there is trouble that can be solved by refactoring. You will have to develop your own sense of how many instance variables is too manyinstance variables and how many lines of code in a method are too many lines. -- Martin Fowler 1999, p75.

Ultimately, Christopher Alexander is lurking beneath this idea:

As far as I am concerned, patterns and XP are "the first try" and "the second try" of Kent Beck to put Christopher Alexander's ideas into practice. Both of them are derivatives of the Alexanderian philosophy.
On Kent's second try, he very carefully avoided mentioning Alexander. Perhaps he thought that Alexander just confused people, and that it was better to avoid ideas like the QWAN. So, he invented a whole bunch of new vocabulary to convey these ideas, things like "Code smells" and Do the simplest thing that could possibly work. -- Ralph Johnson

Catalogue

A alphabetical collection of code smells:

Links