Slide 1

Slide 1 text

Becoming a better developer by using the SOLID design principles How to write code that’s easy to maintain, extend and understand

Slide 2

Slide 2 text

Katerina Trajchevska ● Senior Software Engineer & co-founder of Adeva ● Remote Work Advocate ● Community Volunteer ● Consultant with startups and Fortune 500 companies Software Engineer and co-founder of Adeva

Slide 3

Slide 3 text

@ktrajchevska Working on legacy code ● Re-reading code multiple times to get to the part you need to change ● Hard to understand what a method does ● Spending a lot of time to fix a minor bug ● You spend more time reading than writing code.

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

@ktrajchevska Working on a startup product ● In charge of the development process ● Constantly adding new features ● No formal process ● Very dynamic environment, no time to worry about code structure ● What is it like to go back to your code after 2 years?

Slide 6

Slide 6 text

@ktrajchevska What will we talk about today? ● What is SOLID and how it can make our lives easier ● What’s the purpose of each of the SOLID design principles ● How to not use SOLID

Slide 7

Slide 7 text

@ktrajchevska The purpose of SOLID design principles ● To make the code more maintainable. ● To make it easier to quickly extend the system with new functionality without breaking the existing ones. ● To make the code easier to read and understand, thus spend less time figuring out what it does and more time actually developing the solution. ● Introduced by Robert Martin (Uncle Bob), named by Michael Feathers.

Slide 8

Slide 8 text

Single Responsibility Principle A class should have one, and only one, reason to change.

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

@ktrajchevska Single Responsibility Principle ● A class should only be responsible for one thing. ● There’s a place for everything and everything is in its place. ● Find one reason to change and take everything else out of the class. ● Very precise names for many small classes > generic names for large classes.

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Open/Closed Principle An entity should be open for extension, but closed for modification.

Slide 16

Slide 16 text

@ktrajchevska Open/Closed Principle ● An entity should be open for extension but closed for modification. ● Extend functionality by adding new code instead of changing existing code. ● Separate the behaviors, so the system can easily be extended, but never broken. ● Goal: get to a point where you can never break the core of your system.

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

@ktrajchevska Liskov Substitution Principle ● Any derived class should be able to substitute its parent class without the consumer knowing it. ● Every class that implements an interface, must be able to substitute any reference throughout the code that implements that same interface. ● Every part of the code should get the expected result no matter what instance of a class you send to it, given it implements the same interface.

Slide 26

Slide 26 text

Image credits: Derick Bailey

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

@ktrajchevska Interface Segregation Principle ● A client should never be forced to depend on methods it doesn't use. ● Or, a client should never depend on anything more than the method it’s calling. ● Changing one method in a class shouldn’t affect classes that don’t depend on it. ● Replace fat interfaces with many small, specific interfaces.

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Image credits: Derick Bailey

Slide 38

Slide 38 text

@ktrajchevska Dependency Inversion Principle ● Never depend on anything concrete, only depend on abstractions. ● High level modules should not depend on low level modules. They should depend on abstractions. ● Able to change an implementation easily without altering the high level code.

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

@ktrajchevska ⚠ Don’t get trapped by SOLID ● SOLID design principles are principles, not rules. ● Always use common sense when applying SOLID. ● Avoid over-fragmenting your code for the sake of SRP or SOLID. ● Don’t try to achieve SOLID, use SOLID to achieve maintainability.

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

@ktrajchevska Final Thoughts ● The purpose of SOLID principles is to make your code more maintainable, easy to extend and reason about. ● It requires spending more time writing code, so you can spend less reading it later. ● SOLID principles are principles, not rules. ● Always know your trade-offs and use common sense. ● SOLID is your tool, not your goal.

Slide 47

Slide 47 text

Thank you @ktrajchevska Connecting tech companies with world-class developers