solid and clean code with life project example
TRANSCRIPT
![Page 1: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/1.jpg)
Datascope Systems Ltd.
How to write meaningful code
![Page 2: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/2.jpg)
Bullet Points
SOLID• Single Responsibility Principle• Open/Closed Principle• Liskov Substitution Principle• Inversion Of control• Dependency Inversion Principle
POLA Demeter Law Lowering Principle Live project example
![Page 3: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/3.jpg)
Single Responsibility principle
The class has only one reason to change! Repository – is that a good example? What if
persistence mechanism for saving/retrieving business entities will be changed separately? CQRS!
Facade pattern for interacting with printing subsystem.
![Page 4: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/4.jpg)
RFID Facade example
Class has more than 1 responsibility, but only one reason to change.
![Page 5: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/5.jpg)
Open-Closedprinciple
![Page 6: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/6.jpg)
Open-Closedprinciple
Modules should be open for extension but closed to modification
![Page 7: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/7.jpg)
Barbara-Liskov substitution principle
x(O) = y(O) if Y is type of T, and T is derived from X a subtype of X
![Page 8: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/8.jpg)
Interface Segregation Principle
![Page 9: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/9.jpg)
Interface Segregation Principle
Let’s imagine we have an interface which is pretty much the same as a sub-repository
![Page 10: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/10.jpg)
This is how saving/loading supposed to look like
And Boooooom, at some point business requires only read-only settings
Interface Segregation Principle
![Page 11: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/11.jpg)
Load probably will be similarly to that then …
And start from here we have breach ISP. Client should not depend on any method he is not using
Interface Segregation Principle
![Page 12: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/12.jpg)
Solution: split into two interfaces
Interface Segregation Principle
![Page 13: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/13.jpg)
Dependency InversionPrinciple
![Page 14: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/14.jpg)
Dependency InversionPrinciple
Example of inversion of control in DB is a many to many relation. Order has no idea of OrderDetails existence. As long as you have a magic word
1) using i.e C# 2) import i.e. Java 3) include i.e C,C++
You have an arrow pointing from you (you are depend on smth…)
![Page 15: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/15.jpg)
Examples
Time to go Real…
![Page 16: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/16.jpg)
Let’s be interactive
![Page 17: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/17.jpg)
DeliveryCollectionHelper vs POLA and SRP
ФабричноЛоггеровыйКартинкоДоставочноСервисныйХелпер
![Page 18: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/18.jpg)
Factory OR DI?
ЗависимостиИнжектируемаяСинглтоноФабрика
![Page 19: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/19.jpg)
Let’s apply some refactoringGet rid of unnecessary dependencies and apply SRP. Lowering Law, Internal Coupling,Descriptors and Views (Logical diff), Refactoring stages
![Page 20: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/20.jpg)
OCP
Unnecessary coupling (Class contains two services), horizontal dependencies, try to avoid that
![Page 21: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/21.jpg)
Strategy and Factory
![Page 22: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/22.jpg)
Strategy and Factory
![Page 23: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/23.jpg)
Strategy is ready, but still something is wrong??? Proper Factory
![Page 24: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/24.jpg)
Usage of Factory and Strategies example
![Page 25: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/25.jpg)
Test is pretty damn simple
![Page 26: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/26.jpg)
What`s wrong here?
Class usage example:
Class definition (object will goes down to service layer):
![Page 27: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/27.jpg)
Demeter law in action
• Each unit should have only limited knowledge about other units: only units "closely" related to the current unit.
• Each unit should only talk to its friends; don't talk to strangers.• Only talk to your immediate friends.
![Page 28: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/28.jpg)
Demeter law in action
![Page 29: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/29.jpg)
What about up/down casting through explicit operator? Hide SP’s - Sort of ACL
![Page 30: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/30.jpg)
Right way of doing it (this is service layer)
![Page 31: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/31.jpg)
Testability problem. Action chain solution
![Page 32: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/32.jpg)
Action chain usage
![Page 33: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/33.jpg)
ISP in action
![Page 34: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/34.jpg)
ISP in action
![Page 35: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/35.jpg)
DI in action
![Page 36: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/36.jpg)
DI in action (diff between DIP andDependency Injection)
![Page 37: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/37.jpg)
DI in action (more complicated example)
What if you need sort of magnet objects like reports, user roles, email generators etc.So, how to invert arrows ? How to get rid of using, import etc in target class
![Page 38: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/38.jpg)
DI in action (report example)
Dependency diagram you want to achieve:
![Page 39: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/39.jpg)
DI in action (report example)Reports side effect (two reports 99% equals)
This is how it looks like in practice:
![Page 40: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/40.jpg)
Report application layer
![Page 41: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/41.jpg)
Report Collector in action
![Page 42: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/42.jpg)
My LSP mistake in Quilt project
![Page 43: SOLID and Clean Code with life project example](https://reader035.vdocuments.mx/reader035/viewer/2022070600/589f392e1a28ab490c8b4bf1/html5/thumbnails/43.jpg)
Software Development is amazing!