My Adsense

Sunday, February 14, 2010

Code Smells : The Manual [Part 1]

       Code Smells
By the definition i found in 
 Wikipedia, code smell is: "In computer programmingcode smell is any symptom in the source code of a program that possibly indicates a deeper problem."
We do mistakes while we code. Its very certain. Some of us know about the smell while we code, others don't. But if someone just avoid the common smells in coding, his/her code will be increased a lot in quality. So, to write a quality code, we must know what are the code smells and we MUST avoid it. 
My personal opinion is, don't be a procrastinator in coding. Remove the smells after a single method or at least after completing a feature. 
In below these are the code smells that a beginner in coding should know. I will add more explanation to each of the smells in future. 
[Black circled point is the name of the smell and while circled point is the solution to remove the smell from your code.]
  • Inappropriate naming
    • rename it
  • Comments
    • extract method
    • rename method
    • introduce assertion
  • Dead code
    • delete
  • Duplicated code
    • extract method
    • pull up field
    • form template method
    • substitute algorithm
  • Primitive obsession
    • extract class
    • replace data value with object
    • replace type code with class
    • introduce parameter object
    • replace array with object
  • Large class
    • extract class
    • replace type code with class/subclass
    • replace type code with state/strategy
    • replace conditional with polymorphism
    • extract interface
    • duplicate observer data
  • Lazy class
    • inline class
    • collapse hierarchy
  • Long method
    • extract method
    • replace temp with query
    • introduce parameter object
    • preserve whole object
    • replace method with method object
    • decompose conditional  
  • Long parameter list
    • introduce parameter object
    • replace parameter with method
    • preserve whole object
  • Switch statements
    • replace type code with polymorphism
    • replace type code with state/ strategy
    • replace parameter with explicit methods
    • introduce null object
  • Speculative generality
    • collapse hierarchy
    • inline class
    • remove parameter
    • rename method
  • Feature envy
    • move field
    • move method
    • extract method
  • Temporary field
    • extract method

No comments: