Session => Refactoring to SOLID CodeWhat You must already know:- Object Oriented Programming Language such as Java, C++, C#- Professional ExperienceWhat You will learn today:- Good Practices- OO PrinciplesSponsors:
Speaker => Adil Ahmed MughalAffiliations:Contact Information: firstname.lastname@example.org @adilamughal http://www.adilmughal.com
Agenda This is not a single side talk, its discussion! Promise me that you will raise hand when you have query?• How You Code Today?• What is Bad Design?• What would You Call a Good Design?• SOLID Principles!
How You Code Today?• Do you care about design principles or patterns?• Anti-patterns?• Is your code Agile?• Do you Refactor your code?• Is your code design bad or good?
Bad design is... Needless Rigid Complexity Needless Fragile RepetitionImmobileViscous
What would You Call a Good Design?• Reliability• Flexibility• Separations of Concerns• Reusability• Maintainability
SOLID Object Oriented PrinciplesIntroduced by Robert C. Martin in early 2000s
SOLID Principles• S – Single Responsibility Principle (SRP)• O – Open Closed Principle (OCP)• L – Liskov Substitution Principle (LSP)• I – Interface Segregation Principle (ISP)• D – Dependency Inversion Principle (DIP)
The Single Responsibility Principle"A responsibility is a reason to change, a class ormodule should have one, and only one, reasonto change."
The Single Responsibility Principle• “There should never be more than one reason for a class to change.”• More responsibilities => More likelihood of change• Dijkstra’s SoC: separation of concerns• Applies on every level of code• Solves the naming dilemma
Trade Validator Trade Validator DBTrade What is wrong here: Changes if DB changes or Business Logic Changes
Refactored Trade Validator Trade Trade Validator SourceTrade DB Whats its job? Classes must have an identifiable single responsibility.
The Open Closed Principle“Software Entities (Classes, Modules, Functions,etc.) should be open for extension, but closed formodification”
The Open Closed Principle• Modules that conform to the open-closed principle have two primary attributes – Open For Extension • behavior of the module can be extended – Closed for Modification • The source code of such a module is inviolate• The normal way to extend the behavior of a module is to make changes to that module. How can these two opposing attributes be resolved?
OCP – Change behavior without changing code?• Abstraction is the key to achieve it Client Server Closed Client
OCP – Change behavior without changing code?Client Abstract Server Server Open Client
Dependency Inversion Principle“High level modules should not depend upon lowlevel modules. Both should depend uponabstractions. ““Abstractions should not depend upon details.Details should depend upon abstractions.”
Trade Validator Introduce stabilityHigh Level (Less Stable) with abstraction Trade Trade Validator Source Trade Low Level DB (More Stable)
Better Trade ValidatorTrade Validator <Interface> ITradeSourceTrade DbTradeSource DB
Extensible Trade Validator <Interface>TradeValidator ITradeSourceTrade DbTradeSource WSTradeSource DB Cloud
Actions => From Now On Learn, Practice and Make it your HabitLinks:- ObjectMentor.com- Practices and Patterns on MSDNBooks:
References• Combatting Software Entropy using Design Principles and Practices – Hammad Rajjoub – TechEd ME• Agile Principles, Patterns and Practices using C# by Robert.C.Martin• Special thanks for motivational poster images by NimblePros
Thank You!Refactoring to SOLID Code Emerging .NET Devs