solid principles part 1
TRANSCRIPT
IntroductionA Little background
Maulik Soni
Few Terminologies
Basic OOP Cohesion Coupling
Code Smells Design Smells Technical Debt
Maulik Soni
Cohesion
Cohesion refers to what the class (or module) will do Try to achieve High Cohesion
Maulik Soni
Coupling
How related are two classes / modules and how dependent they are on each other Try to achieve Loose Coupling
Maulik Soni
Code Smells
Duplicate code Long method Large class Temporary field Switch statements Parallel inheritance hierarchies
Maulik Soni
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
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
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
End of Introduction
So Now, Let’s begin…
Maulik Soni
SOLID Principles - ISRP – Single Responsibility PrincipleO/CP – Open Closed Principle
Maulik Soni
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.
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
Maulik Soni
Why Solid ? Another View
To Deliver Fast To manage changes easily To deal with complexity
Maulik Soni
Single Responsibility Principle
A software module should have one and only one responsibility
Maulik Soni
Example
Maulik Soni
Is SRP Violated Here ?
Maulik Soni
Is SRP Violated Here ?
Maulik Soni
Real world SRP
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
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
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
Maulik Soni
Is O/CP Violated Here ?
Maulik Soni
Solution
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
Maulik Soni
End of Presentation