Introduction to AntiPatterns & CodeSmells
-
Upload
claudio-bernasconi -
Category
Technology
-
view
2.183 -
download
0
description
Transcript of Introduction to AntiPatterns & CodeSmells
AntiPatterns & CodeSmells
Common practices to improve code quality
Anti patterns
Counterpart to design patterns
What is an Anti pattern? The result of a developer not knowing any
better, not having sufficient knowledge or experience in solving a particular type of problem.
Or a developer has applied a perfectly good pattern in the wrong context.
The Blob Also known as: «The God Class» Root Causes: Sloth, Haste Anecdotal Evidence: “This is the class that
is really the 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 no longer with the company) wrote that routine back when Jim (who left last month) was trying a workaround for Irene’s input processing code (she’s in another department now, too). I don’t think it’s used
anywhere now, but I’m not really sure. Refactoring: Architectural Configuration
Management
Golden Hammer Definition: A Golden Hammer is a familiar
technology or concept applied obsessively to many software problems.
Root Causes: Ignorance, Pride, Narrow-Mindedness
Anecdotal Evidence: “I have a hammer and everything is a nail.”, “Our database is our architecture.”
Refactoring: Expand your horizons
Code smells
Give 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
be refactored. Tries to explain not only you should refactor,
but why you should do so.
Duplicated codeDefinition (Nearly) the same
code structure in more than one place
Refactorings Extract Method Extract Class Pull Up Field Template Method
Shotgun SurgeryDefinition Every time you make
a kind of change, you have to make a lot of little changes to a lot of different classes
Refactorings Move Method Move Field Inline Class
Switch StatementsDefinition The problem with
switch statements is essentially that of duplication.
Often you find the same switch statement over and over again.
If you add a new clause to the switch, you have to find all the other switches and change them.
Refactorings Replace with
Strategy Replace with State Replace Conditional
with Polymorphism
Data classDefinition Class that has only
fields, no methods operating on these fields.
Contains fields which are manipulated from other classes
Refactorings 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 of existing code Martin Fowler (1963)
Author and international speaker on software development, specialized in OO design.
Kent Beck (1961) Software engineer and the
creator of Extreme Programming and TDD software development methodologies.
Publication date: 28 Jun 1999
ISBN-13: 978-0201485677