connyIsTired / ideas

A place to store ideas, topics to research, future project ideas, questions to answer, etc.
0 stars 0 forks source link

SOLID principles #1

Open connyIsTired opened 2 years ago

connyIsTired commented 2 years ago

Learn more about solid principles

Single-responsibility principle Open-closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle

connyIsTired commented 2 years ago

Learned about SRP Each module, or piece of code should be responsible for a single element of program functionality. The Email module should not also create a task. Keep things encapsulated, as decoupled as possible, and focused on one piece of business functionality. Additionally, consider the example you read about with the space station reporting functionality. Both fuel levels and supplies have to provide reports. Having a parent reporting class and then sub reporting classes further separates and encapsulates each piece of functionality and business logic. First with a reporting class and then a specific implementation of how reports are generated.

connyIsTired commented 2 years ago

How I understand Open Closed is that objects should use composition instead of inheritance. Composition - Car HAS A engine. Inheritance - Car IS A auto-mobile.

Open Close definition is Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.

Additionally, think about interfaces vs just reusing code from a superclass. inheritance from superclasses is rigid from a subclass perspective and fragile from a superclass perspective. Interfaces that simply use a superclass/backend or more easily changed as code requirements change.

https://www.artima.com/articles/composition-versus-inheritance

I would describe this principle as the ability to easily and safely change code as requirements change, specifically when talking about inheritance and composition.

Always choose composition when you can.