1 of 19

Refactoring your legacy code using SOLID principles

Muhammad Sumon Molla Selim�@SumonMSelim

2 of 19

3 of 19

Working with legacy code

  • Lots of reading and exploring
  • Time consuming
  • Hard to understand

4 of 19

5 of 19

6 of 19

Why SOLID?

  • Maintainable codebase
  • Easy to extend and modify
  • Easy to read and understand

7 of 19

Single Responsibility Principle

Gather together the things that change for the same reasons. Separate those things that change for different reasons.

8 of 19

Single Responsibility Principle

  • A class should be responsible for only one thing
  • Have only one reason to change

9 of 19

Open/Closed Principle

You should be able to extend the behavior of a system without having to modify that system.

10 of 19

Open/Closed Principle

  • Classes, modules, functions, etc. should be open for extension, but closed for modification
  • Extend functionality by adding new code instead of modifying existing code

11 of 19

Liskov Substitution Principle

Let φ(x) be a property provable about objects x of type T. Then φ(y) should also be true for objects y of type S where S is a subtype of T.

12 of 19

Liskov Substitution Principle

  • Any derived class should be able to substitute its parent class
  • Every class that implements an interface must be able to substitute any reference throughout the code that implements the same interface

13 of 19

Interface Segregation Principle

No client should be forced to depend on methods it does not use.

14 of 19

Interface Segregation Principle

  • Tailor interfaces to individual client interface
  • Changing one method in a class shouldn't affect classes that don't depend on it
  • Replace fat interfaces with many small, specific interfaces

15 of 19

Dependency Inversion Principle

High-level modules should not depend on low-level modules. Both should depend on abstractions.

16 of 19

Dependency Inversion Principle

  • Never depend on anything concrete, only depend on abstractions
  • Able to change an implementation easily without altering the high level code

17 of 19

REMEMBER

  • These are only principles, not rules
  • There is no hard rules that you will need to implement all of these
  • Use your common sense to utilize these principles in your codebase properly

18 of 19

Resources

19 of 19

Muhammad Sumon Molla Selim

Senior Software Engineer�AWS Community Builder�@SumonMSelim

https://www.sumonselim.com