15
Code smell & Refactoring Mamata Gelanee

Code smell & refactoring

Embed Size (px)

Citation preview

Page 1: Code smell & refactoring

Code smell & RefactoringMamata Gelanee

Page 2: Code smell & refactoring

Code Smell are certain structures in the code that indicate violation of

fundamental design principles and negatively impact design quality.

Refactoring is a disciplined technique for restructuring an existing body of code,

altering its internal structure without changing its external behavior.

Page 3: Code smell & refactoring

“A code smell is a surface indication that usually corresponds to a deeper problem in the system”

- Martin Flower

Page 4: Code smell & refactoring

Design PrinciplesEvery OO Designer should have follow below design principle, failing to that leads to

code smells in your software.

Design Principles:

- Single Responsibility

- Open Close principle

- Interface Segregation

- Dependency Inversion principle

- Liskov Substitution principle

Page 5: Code smell & refactoring

Levels of Code smell

There are three levels of

code smell:

1. Method level

2. Class level

3. Application level

Page 6: Code smell & refactoring

Bad smells in a codeLong Method

- Extract method

Long class

- Extract Class

Long parameter list

- Introduce Object parameter

Primitive Obsession

- Introduce Object parameter

Duplicate Code (Don’t Repeat Yourself)

- Create Template method

Page 7: Code smell & refactoring

Switch statements

Replace conditional with

polymorphism

Page 8: Code smell & refactoring

Oddball Solution

Move method

or

Move class

Page 9: Code smell & refactoring

Black sheep

Move Method

Page 10: Code smell & refactoring

Refused Bequest

1. Throw exceptions

2. Push down method or field

Page 11: Code smell & refactoring

Speculative Generality

Oh! We might need this feature in future

Remedy:

- Collapse Hierarchy

- Inline class

- Remove Parameter

Page 12: Code smell & refactoring

Quiz

Long class

Page 13: Code smell & refactoring

Quiz

Long Parameter list

Page 14: Code smell & refactoring

Questions?

Page 15: Code smell & refactoring

Thanks!Software Engineer,

Mastercard Transaction solution pvt. Ltd.,

Vadodara - Gujarat

AndiGeeky

@MamataGelanee

andigeeky.wordpress.com/

mamatagelanee07