Code smell & refactoring

Preview:

Citation preview

Code smell & RefactoringMamata Gelanee

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.

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

- Martin Flower

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

Levels of Code smell

There are three levels of

code smell:

1. Method level

2. Class level

3. Application level

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

Switch statements

Replace conditional with

polymorphism

Oddball Solution

Move method

or

Move class

Black sheep

Move Method

Refused Bequest

1. Throw exceptions

2. Push down method or field

Speculative Generality

Oh! We might need this feature in future

Remedy:

- Collapse Hierarchy

- Inline class

- Remove Parameter

Quiz

Long class

Quiz

Long Parameter list

Questions?

Thanks!Software Engineer,

Mastercard Transaction solution pvt. Ltd.,

Vadodara - Gujarat

AndiGeeky

@MamataGelanee

andigeeky.wordpress.com/

mamatagelanee07

Recommended