Introduction to AntiPatterns & CodeSmellsPresentation Transcript
AntiPatterns & CodeSmellsCommon practices to improve code quality
Anti patternsCounterpart to design patterns
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.
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
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
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
Code smellsGive you a hint when something is wrong
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.
Duplicated codeDefinition (Nearly) the same codestructure in more thanone placeRefactorings Extract Method Extract Class Pull Up Field Template Method
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
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
Data classDefinition Class that has onlyfields, no methodsoperating on thesefields. Contains fields whichare manipulated fromother classesRefactorings Encapsulate Fields Encapsulate Collection Move Method Extract Method
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
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