solid principles part 1

25
Introduction A Little background Maulik Soni

Upload: maulik-soni

Post on 15-Apr-2017

199 views

Category:

Software


0 download

TRANSCRIPT

Page 1: SOLID Principles Part 1

IntroductionA Little background

Maulik Soni

Page 2: SOLID Principles Part 1

Few Terminologies

Basic OOP Cohesion Coupling

Code Smells Design Smells Technical Debt

Maulik Soni

Page 3: SOLID Principles Part 1

Cohesion

Cohesion refers to what the class (or module) will do Try to achieve High Cohesion

Maulik Soni

Page 4: SOLID Principles Part 1

Coupling

How related are two classes / modules and how dependent they are on each other Try to achieve Loose Coupling

Maulik Soni

Page 5: SOLID Principles Part 1

Code Smells

Duplicate code Long method Large class Temporary field Switch statements Parallel inheritance hierarchies

Maulik Soni

Page 6: SOLID Principles Part 1

Design Smells

Rigidity Software is difficult to change

Fragility Program breaks in many places when a change mode in a single place

Immobility Parts could be useful in other systems, but effort and risk to separate from original system is

too great.

Maulik Soni

Page 7: SOLID Principles Part 1

Design Smells

Viscosity Design-preserving methods are more difficult to use than the hacks Development environment is slow and inefficient

Needless complexity Contains elements that aren’t currently useful

Needless repetition System has lots of repeated code elements

Opacity A module is difficult to understand

Maulik Soni

Page 8: SOLID Principles Part 1

Technical Debt

The cost to fix rotting code Interest charges build over time The longer we take to remove the smells, the more it will cost

Maulik Soni

Page 9: SOLID Principles Part 1

End of Introduction

So Now, Let’s begin…

Maulik Soni

Page 10: SOLID Principles Part 1

SOLID Principles - ISRP – Single Responsibility PrincipleO/CP – Open Closed Principle

Maulik Soni

Page 11: SOLID Principles Part 1

What are SOLID Principles ?

Single responsibility Open-closed Liskov substitution Interface segregation Dependency inversion

Maulik Soni

First five principles named by Robert C Martin in early 2000.

Page 12: SOLID Principles Part 1

Maulik Soni

Why SOLID ?

When Applied together it creates a system Easy to Maintain Easy to Extend over time

To remove the Code Smells Part of overall strategy of agile and adaptive programming

Page 13: SOLID Principles Part 1

Maulik Soni

Why Solid ? Another View

To Deliver Fast To manage changes easily To deal with complexity

Page 14: SOLID Principles Part 1

Maulik Soni

Single Responsibility Principle

A software module should have one  and only one responsibility

Page 15: SOLID Principles Part 1

Maulik Soni

Example

Page 16: SOLID Principles Part 1

Maulik Soni

Is SRP Violated Here ?

Page 17: SOLID Principles Part 1

Maulik Soni

Is SRP Violated Here ?

Page 18: SOLID Principles Part 1

Maulik Soni

Real world SRP

Page 19: SOLID Principles Part 1

Maulik Soni

SRP - Summary

SRP is the simplest of the principles, and one of the hardest to get right We tend to join responsibilities together It's usually hard to see different  responsibilities

Page 20: SOLID Principles Part 1

Maulik Soni

Open / Closed Principle

All systems change during their life cycles avoid a cascade of changes to dependent modules When requirements change, you extend the behavior, not changing old code

Page 21: SOLID Principles Part 1

Maulik Soni

Open / Closed Principle

Software Entities ( Classes, Modules, Functions Etc.) Should be open for extension but closed for modification

Open for extension Behavior of the module can be extended We are able to change what the module does

Closed for modification Extending behavior does not result in changes to source, binary, or code of the module

Page 22: SOLID Principles Part 1

Maulik Soni

Is O/CP Violated Here ?

Page 23: SOLID Principles Part 1

Maulik Soni

Solution

Page 24: SOLID Principles Part 1

Open / Closed Rule of Thumb

Use IF/SWITCH statements if number of cases are unlikely to change

Use strategy pattern when number of cases are likely to change

Don’t code for situations that you won’t ever need More smaller class files != more complicated code

Maulik Soni

Page 25: SOLID Principles Part 1

Maulik Soni

End of Presentation