Introduction to AntiPatterns & CodeSmells


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction to AntiPatterns & CodeSmells

  1. 1. AntiPatterns & CodeSmellsCommon practices to improve code quality
  2. 2. Anti patternsCounterpart to design patterns
  3. 3. What is an Anti pattern? The result of a developer not knowing any better, nothaving sufficient knowledge or experience in solvinga particular type of problem. Or a developer has applied a perfectly good patternin the wrong context.
  4. 4. The Blob Also known as: «The God Class» Root Causes: Sloth, Haste Anecdotal Evidence: “This is the class that is reallythe heart of our architecture:” Refactoring: Refactoring of Responsibilities
  5. 5. Lava Flow Also known as: Dead Code Root Causes: Greed, Sloth Anecdotal Evidence: “Oh that! Well Ray and Emil (they’re nolonger with the company) wrote that routine back when Jim (who leftlast month) was trying a workaround for Irene’s input processingcode (she’s in another department now, too). I don’t think it’s usedanywhere now, but I’m not really sure. Refactoring: Architectural ConfigurationManagement
  6. 6. Golden Hammer Definition: A Golden Hammer is a familiartechnology or concept applied obsessively to manysoftware problems. Root Causes: Ignorance, Pride, Narrow-Mindedness Anecdotal Evidence: “I have a hammer andeverything is a nail.”, “Our database is ourarchitecture.” Refactoring: Expand your horizons
  7. 7. Code smellsGive you a hint when something is wrong
  8. 8. What is a Code smell? If it stinks, change it. Helps you to spot a code portion which should berefactored. Tries to explain not only you should refactor, but whyyou should do so.
  9. 9. Duplicated codeDefinition (Nearly) the same codestructure in more thanone placeRefactorings Extract Method Extract Class Pull Up Field Template Method
  10. 10. Shotgun SurgeryDefinition Every time you make akind of change, youhave to make a lot oflittle changes to a lot ofdifferent classesRefactorings Move Method Move Field Inline Class
  11. 11. Switch StatementsDefinition The problem with switchstatements is essentiallythat of duplication. Often you find the sameswitch statement overand over again. If you add a new clauseto the switch, you haveto find all the otherswitches and changethem.Refactorings Replace with Strategy Replace with State Replace Conditionalwith Polymorphism
  12. 12. Data classDefinition Class that has onlyfields, no methodsoperating on thesefields. Contains fields whichare manipulated fromother classesRefactorings Encapsulate Fields Encapsulate Collection Move Method Extract Method
  13. 13. Overview Bad Smells in Code Bad Smells in Code Duplicated Code Long Method Large Class Long Parameter List Divergent Change Shotgun Surgery Feature Envy Data Clumps Primitive Obsession Switch Statements Parallel Inheritance Hierarchies Lazy Class Speculative Generality Temporary Field Message Chains Middle Man Inappropriate Intimacy Alternative Classes with Different Interfaces Incomplete Library Class Data Class Refused Bequest Comments Development AntiPatterns The Blob Continuous Obsolescence Lava Flow Ambiguous Viewpoint Functional Decomposition Poltergeists Boat Anchor Golden Hammer Dead End Spaghetti Code Input Kludge Walking through a Minefield Cut-And-Paste Programming Mushroom Management
  14. 14. Refactoring – Improving the design ofexisting code Martin Fowler (1963) Author and international speakeron software development,specialized in OO design. Kent Beck (1961) Software engineer and thecreator of Extreme Programmingand TDD software developmentmethodologies. Publication date: 28 Jun1999 ISBN-13: 978-0201485677
  15. 15.