15
AntiPatterns & CodeSmells Common practices to improve code quality

Introduction to AntiPatterns & CodeSmells

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Introduction to AntiPatterns & CodeSmells

AntiPatterns & CodeSmells

Common practices to improve code quality

Page 2: Introduction to AntiPatterns & CodeSmells

Anti patterns

Counterpart to design patterns

Page 3: Introduction to AntiPatterns & CodeSmells

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.

Page 4: Introduction to AntiPatterns & CodeSmells

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

Page 5: Introduction to AntiPatterns & CodeSmells

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

Page 6: Introduction to AntiPatterns & CodeSmells

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

Page 7: Introduction to AntiPatterns & CodeSmells

Code smells

Give you a hint when something is wrong

Page 8: Introduction to AntiPatterns & CodeSmells

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.

Page 9: Introduction to AntiPatterns & CodeSmells

Duplicated codeDefinition (Nearly) the same

code structure in more than one place

Refactorings Extract Method Extract Class Pull Up Field Template Method

Page 10: Introduction to AntiPatterns & CodeSmells

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

Page 11: Introduction to AntiPatterns & CodeSmells

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

Page 12: Introduction to AntiPatterns & CodeSmells

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

Page 13: Introduction to AntiPatterns & CodeSmells

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

Page 14: Introduction to AntiPatterns & CodeSmells

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

Page 15: Introduction to AntiPatterns & CodeSmells

Resources

www.SourceMaking.com