Single Responsibility Principle Explained - SOLID Design Principles

preview_player
Показать описание
🚨 IMPORTANT:

Use code KYLE for an additional $50

Writing clean code is tough, and is one of the largest differences between junior and senior developers. One way that you can quickly improve your code and start writing well designed code now is to implement the single responsibility principle. This principle is part of the SOLID design principles and is, in my opinion, the most important rule of SOLID. This is because it is easy to implement and test for while still being incredibly useful in terms of making your code cleaner.

The main idea of the single responsibility principle is that every class/function/module/section of your code should have only one responsibility. This means that the code should only have one single reason to change. If there are two different reasons for the code to change, or if it does two distinctly different things then it is in violation of this principle and is potentially poorly designed code.

Most likely you have written tons of code in violation of this principle, I know I have, but hopefully this video will help introduce you to why this principle is so good at making your code clean and maintainable.

🧠 Concepts Covered:

- What the single responsibility principle is
- Why the single responsibility principle is important
- How to use the single responsibility principle
- How to spot violations of the single responsibility principle

🌎 Find Me Here:

#SOLID #WDS #SingleResponsibilityPriniciple
Рекомендации по теме
Комментарии
Автор

A class with a single method is a code smell. The solution is typically a closure.
Your example also exposed that log Message wasn't a good name for a function that sends an email. What happens as a result of exceeding calorie count is intimate knowledge and none of the class' business. Use an event or signal.

I like your videos. Thanks for your work.

skewty
Автор

This is so cool. I'm working on an app that makes 100s of API calls to different microservices.
I have to show some sort of messagebox or alert in my axios catch if it fails or out of bounds.... I could have hardcoded a messagebox but now the client wants to log the messages and API them back to a server, so management can see them on a dashboard. Imagine having to add all those statements :lol:. I call an error function and can add a few lines of code now. We may want to move away from bootstrap dialogs, it would be really easy.


You don't realize how important SOLID is until you need it! If you are duplicating any code consider making a function!

alb
Автор

I think that this might be a little clearer if you explained the difference between having more than one method in a class and having more than one responsibility in a class. Helpful video, though.

brianjlevine
Автор

kyle: let me know if you wanna...


me: yes

covertgravy
Автор

These are things that they don't teach in other tutorials. Thanks Kyle for sharing this solid design principle.

kelvezu
Автор

Please make more video about SOLID Principle. You're tutorials is always a big help. I have learn a lot. Thank you

kerveybrillante
Автор

In this case, I actually think that the new code is harder to read and maintain, because one function (track calories) suddenly does two things: Tracking AND notifying.

leokr
Автор

This is such a great channel. I will be spreading the word as much as I can. Thanks for all the amazing content. I am only two years into my dev career and stuff like this is helping me think about how I code better.

jasonwelsh
Автор

The final class still accumulates trajectories and keeps track of the value, compares it with a limit and logs a warning. All what changed is that it does not call the system log function, but does it indirectly through a custom function (which is a good change, but not sufficient if you really want the SR rule).

tomaskot
Автор

Please cover the other letters for sure! Very useful to split them up and not give too much info.. thanks!

PP-sszf
Автор

Having classes with one method literally defeats the purpose of using a class. I feel like cohesion is more important than strictly adhering to the single responsibility principle. In this example the trackCalories and logCalorieSurplus are cohesive, so I don’t see a problem with keeping them in the same class

ariassingh
Автор

thanks for the video! and please yes cover all the SOLID priciples.

geo
Автор

Thanks so much...please cover ALL the solid principles. Your explanation is amazing

armaandhanji
Автор

you literally have a video for eveything! Thank you so much!

clipsbyczar
Автор

Thanks. And yes, would be great to cover the other patterns.

oleksandrvorovchenko
Автор

Your English is perfect thanks from Brazil 👏

alvoradaprofética
Автор

Yes, please cover the other letters too. I really like the example.

labwax
Автор

Finally! I understand the S in SOLID. Now for the rest of the acronym :P

this.channel
Автор

Does this imply that a class can only have one method? It doesn't sound right.

cotixmol
Автор

Great video as usual. The SRP can be a bit of a pain though if taken too far. As well as generating more code (in principal) it can lead to overly fragmented code. Personally, I find it’s more practical to think of a responsibility as a “manageable unit”. I.e. group the code in a way which is easily understood, easily maintained, only depends on generic methods and of course is DRY.

m