SOLID and Clean Code with life project example

43
Datascope Systems Ltd. How to write meaningful code

Transcript of SOLID and Clean Code with life project example

Page 1: SOLID and Clean Code with life project example

Datascope Systems Ltd.

How to write meaningful code

Page 2: SOLID and Clean Code with life project example

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

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

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

Open-Closedprinciple

Page 6: SOLID and Clean Code with life project example

Open-Closedprinciple

Modules should be open for extension but closed to modification

Page 7: SOLID and Clean Code with life project example

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

Interface Segregation Principle

Page 9: SOLID and Clean Code with life project example

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

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

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

Solution: split into two interfaces

Interface Segregation Principle

Page 13: SOLID and Clean Code with life project example

Dependency InversionPrinciple

Page 14: SOLID and Clean Code with life project example

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

Examples

Time to go Real…

Page 16: SOLID and Clean Code with life project example

Let’s be interactive

Page 17: SOLID and Clean Code with life project example

DeliveryCollectionHelper vs POLA and SRP

ФабричноЛоггеровыйКартинкоДоставочноСервисныйХелпер

Page 18: SOLID and Clean Code with life project example

Factory OR DI?

ЗависимостиИнжектируемаяСинглтоноФабрика

Page 19: SOLID and Clean Code with life project example

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

OCP

Unnecessary coupling (Class contains two services), horizontal dependencies, try to avoid that

Page 21: SOLID and Clean Code with life project example

Strategy and Factory

Page 22: SOLID and Clean Code with life project example

Strategy and Factory

Page 23: SOLID and Clean Code with life project example

Strategy is ready, but still something is wrong??? Proper Factory

Page 24: SOLID and Clean Code with life project example

Usage of Factory and Strategies example

Page 25: SOLID and Clean Code with life project example

Test is pretty damn simple

Page 26: SOLID and Clean Code with life project example

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

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

Demeter law in action

Page 29: SOLID and Clean Code with life project example

What about up/down casting through explicit operator? Hide SP’s - Sort of ACL

Page 30: SOLID and Clean Code with life project example

Right way of doing it (this is service layer)

Page 31: SOLID and Clean Code with life project example

Testability problem. Action chain solution

Page 32: SOLID and Clean Code with life project example

Action chain usage

Page 33: SOLID and Clean Code with life project example

ISP in action

Page 34: SOLID and Clean Code with life project example

ISP in action

Page 35: SOLID and Clean Code with life project example

DI in action

Page 36: SOLID and Clean Code with life project example

DI in action (diff between DIP andDependency Injection)

Page 37: SOLID and Clean Code with life project example

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

DI in action (report example)

Dependency diagram you want to achieve:

Page 39: SOLID and Clean Code with life project example

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

Report application layer

Page 41: SOLID and Clean Code with life project example

Report Collector in action

Page 42: SOLID and Clean Code with life project example

My LSP mistake in Quilt project

Page 43: SOLID and Clean Code with life project example

Software Development is amazing!