Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Refactoring to SOLID Code


Published on

"Refactoring to SOLID Code" session presentation from
Emerging .NET Devs - October 2011 User Group Meeting.

Please note that this presentation has been simplified for publishing.

Published in: Technology
  • Be the first to comment

Refactoring to SOLID Code

  1. 1. Emerging .NET DevsOctober User Group MeetingDesign Principles &PracticesAdil Ahmed MughalZubair Ahmed
  2. 2. Refactoring to SOLID Code Emerging .NET Devs
  3. 3. 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:
  4. 4. Speaker => Adil Ahmed MughalAffiliations:Contact Information: @adilamughal
  5. 5. 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!
  6. 6. 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?
  7. 7. Bad design is... Needless Rigid Complexity Needless Fragile RepetitionImmobileViscous
  8. 8. What would You Call a Good Design?• Reliability• Flexibility• Separations of Concerns• Reusability• Maintainability
  9. 9. SOLID Object Oriented PrinciplesIntroduced by Robert C. Martin in early 2000s
  10. 10. 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)
  11. 11. Single Responsibility Principle (SRP)
  12. 12. The Single Responsibility Principle"A responsibility is a reason to change, a class ormodule should have one, and only one, reasonto change."
  13. 13. 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
  14. 14. Trade Validator Trade Validator DBTrade What is wrong here: Changes if DB changes or Business Logic Changes
  15. 15. Refactored Trade Validator Trade Trade Validator SourceTrade DB Whats its job? Classes must have an identifiable single responsibility.
  16. 16. The Open-Closed Principle (OCP)
  17. 17. The Open Closed Principle“Software Entities (Classes, Modules, Functions,etc.) should be open for extension, but closed formodification”
  18. 18. 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?
  19. 19. OCP – Change behavior without changing code?• Abstraction is the key to achieve it Client Server Closed Client
  20. 20. OCP – Change behavior without changing code?Client Abstract Server Server Open Client
  21. 21. Liskov Substitution Principle (LSP)
  22. 22. Liskov Substitution Principle“Functions that reference a base class must beable to use objects of derived classes withoutknowing it."
  23. 23. Interface Segregation Principle (ISP)
  24. 24. Interface Segregation Principle“Clients should not be forced to depend uponinterfaces that they do not use.”• Prefer small, cohesive interfaces to fatinterfaces
  25. 25. Dependency Inversion Principle (DIP)
  26. 26. 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.”
  27. 27. Trade Validator Introduce stabilityHigh Level (Less Stable) with abstraction Trade Trade Validator Source Trade Low Level DB (More Stable)
  28. 28. Better Trade ValidatorTrade Validator <Interface> ITradeSourceTrade DbTradeSource DB
  29. 29. Extensible Trade Validator <Interface>TradeValidator ITradeSourceTrade DbTradeSource WSTradeSource DB Cloud
  30. 30. DEMOLet’s Refactor!
  31. 31. Actions => From Now On Learn, Practice and Make it your HabitLinks:- Practices and Patterns on MSDNBooks:
  32. 32. 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
  33. 33. Thank You!Refactoring to SOLID Code Emerging .NET Devs