Single Responsibility Principle Explained - SOLID Design Principles

  Рет қаралды 194,350

Web Dev Simplified

Web Dev Simplified

Күн бұрын

Пікірлер: 190
@kantyDarius
@kantyDarius 5 жыл бұрын
This channel is SO underrated 👏👏👏
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
Thank you! I feel I have grown a ton since I just started about a year and a half ago.
@TF23DayRespawn
@TF23DayRespawn 4 жыл бұрын
No kidding, everything from this channel screams 'quality'
@WebDevSimplified
@WebDevSimplified 4 жыл бұрын
@@TF23DayRespawn Thank you so much!
@Bargains20xx
@Bargains20xx 3 жыл бұрын
totally agreed, he has a very good way of explaining stuff.
@igorrohlik
@igorrohlik 3 жыл бұрын
could not agree more!
@covertgravy
@covertgravy 5 жыл бұрын
kyle: let me know if you wanna... me: yes
@thatoneuser8600
@thatoneuser8600 4 жыл бұрын
wut
@deansummer1356
@deansummer1356 4 жыл бұрын
@@thatoneuser8600 lul
@pahvalrehljkov
@pahvalrehljkov 4 жыл бұрын
i havent found any other source that explained so simply and concise, almost everything he explains here just clicks in my head... im enjoying watching his tutorials...
@alb12345672
@alb12345672 5 жыл бұрын
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!
@ariassingh462
@ariassingh462 2 жыл бұрын
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
@SharukhSaifi
@SharukhSaifi 4 ай бұрын
These are principles not laws.
@skewty
@skewty 4 жыл бұрын
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.
@jasonwelsh417
@jasonwelsh417 4 жыл бұрын
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.
@tomaskot9278
@tomaskot9278 2 жыл бұрын
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).
@alexboisseau6170
@alexboisseau6170 11 ай бұрын
Is that create a "Notifier" interface with a method "notify" could add more respect for the SR ? The CalorieTracker could take a third argument in his constructor (maxCal: number, notifier: Notifier, currentCal = 0) and during the instantiation of the class, pass a class which implement the Notifier interface (eg: LogNotifier)?
@leokr4877
@leokr4877 3 жыл бұрын
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.
@umeshhbhat
@umeshhbhat Жыл бұрын
Yeah that is what I was thinking. There should have been a separate function which checks whether calories have been exceeded or not.
@kerveybrillante8210
@kerveybrillante8210 5 жыл бұрын
Please make more video about SOLID Principle. You're tutorials is always a big help. I have learn a lot. Thank you
@brianjlevine
@brianjlevine 5 жыл бұрын
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.
@ellsonmendesYT
@ellsonmendesYT 3 жыл бұрын
I was wondering the same thing, but I think the classes is suposed to have one main function/method/responsibility whatever and maybe other helper methods
@aryalaashaya
@aryalaashaya 3 жыл бұрын
@@ellsonmendesYT hmmm interesting 🤔 never thought about it that way
@geo2465
@geo2465 5 жыл бұрын
thanks for the video! and please yes cover all the SOLID priciples.
@kelvezu1585
@kelvezu1585 5 жыл бұрын
These are things that they don't teach in other tutorials. Thanks Kyle for sharing this solid design principle.
@PP-ss3zf
@PP-ss3zf 5 жыл бұрын
Please cover the other letters for sure! Very useful to split them up and not give too much info.. thanks!
@Userkazt
@Userkazt 2 жыл бұрын
is this mean a class should only have single method ? eg: for a Cart class Add and Remove method should be in two separate classes ?
@wioetuw912
@wioetuw912 3 жыл бұрын
But how do you decide what is a "single responsibility" or "one reason to change"? For example, you could argue that the CalorieTracker class is still responsible for at least two things: handling the tracking and defining what message to log in case the tracked calories exceed max calories. And you can take the argument even further. If a class and a method both need to satisfy the single responsibility principle, then how can a class ever have more than one method? Two different methods represent two different responsibilities so if they are both in the same class, the class must have at least two different responsibilities. Sure, the responsibilities of the class and the methods are on different levels of abstraction but then how do you know what is the correct way to split the program into layers of abstraction? I mean, otherwise I could just put everything into a function called runProgram and claim that the function satisfies the single responsibility principle because its only responsibility is running the program and it only ever needs to change if I wan't to change how the program works. I know that at least part of the answer to these questions is that you just have to use common sense. But the (supposed) usefulness of having an explicitly stated "single responsibility principle" is itself a consequence of the fact that it's very difficult to make correct decisions based on "common sense" unless you are very experienced in whatever you are doing. So I think it would be nice if there was more guidance on how to actually use this and other similar principles in practice.
@fayziyev
@fayziyev 3 жыл бұрын
Agree, if logic as well as the maxCalories should not be the part of the CalorieTracker. The maxCalories is used to decide whether to log or not to log a message, hence it is not part of the CalorieTracker. The CalorieTracker should only have a single property currentCalories, which might be passed to some logger instance or function, which implements an internal logic such as 'if (currentCalories > maxCalories) {// log something}'
@cotixmol
@cotixmol 2 жыл бұрын
Does this imply that a class can only have one method? It doesn't sound right.
@leandroroberto443
@leandroroberto443 3 жыл бұрын
Very well explained !!!
@clipsbyczar
@clipsbyczar 2 жыл бұрын
you literally have a video for eveything! Thank you so much!
@someonemight
@someonemight 2 жыл бұрын
Good explanation. However, IMO to really separate responsibilities, CalorieTracker should call a notifyCaloriesExceeded function and that function would contain the message to output/email.
@HenleyBailey
@HenleyBailey 5 жыл бұрын
Thanks for all your tutorials, really well explained. Your human API is strong. :)
@armaandhanji7151
@armaandhanji7151 5 жыл бұрын
Thanks so much...please cover ALL the solid principles. Your explanation is amazing
@vitiok78
@vitiok78 5 жыл бұрын
You are doing a very good job, Kyle! I understand your explanations better than others, although English is not my native language ))))
@MyTal123
@MyTal123 2 жыл бұрын
@web dev simplified Hi Kyle, on minute ~3:20 you say that in general we will use more modules than classes, can you please elaborate on that?
@bishopshawiii7292
@bishopshawiii7292 5 жыл бұрын
Keep going with the SOLID principles. Awesome content. Thank you
@rebelmachine88
@rebelmachine88 5 жыл бұрын
Great explanation! Would love to see more SOLID principles.
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
Thanks! I am excited to make the rest of them.
@m12652
@m12652 5 жыл бұрын
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.
@this.channel
@this.channel 3 жыл бұрын
Finally! I understand the S in SOLID. Now for the rest of the acronym :P
@mohammedalmukhtar8949
@mohammedalmukhtar8949 5 жыл бұрын
As always, thank you a ton, Kyle! You're an awesome instructor.
@albud6687
@albud6687 5 жыл бұрын
Great intro to SRP - and one of the simplest, most obvious examples there, to answer the question 'why separate concerns' with a recognizable use-case. I do think though, in today's post-SOLID world (OK Solid is still super relevant), you would do us a great service to warn of the evils of overseparation. For example, m12652 hints at this in the comment. "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..." and then Víctor Navarro flat-out commits the error of overseparation when he comments "A question arise from watching the video: Isn't the CalorieTracker still responsible for logging the surplus? ... (see his comment)". It seems like common sense not to make that mistake, but well meaning people just take it to the other extreme. And you end up with multiple objects doing one thing. In Truth, the SRP is a sweet spot between OverCoupling and OverSeparation called 'Cohesion'. OverSeparating can kill as much as putting things in one huge method if you take it to the extremes. Read on .... Bob Martin, the guru who's evangelizing SOLID, recognized the problem in 2015 with this blog post: blog.cleancoder.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html Entire thing is worth a read. Here are some salient quotes: "The Single Responsibility Principle (SRP) states that each software module should have one and only one reason to change. This sounds good, and seems to align with Parnas’ formulation. However it begs the question: What defines a reason to change? .... "Another wording for the Single Responsibility Principle is: Gather together the things that change for the same reasons. Separate those things that change for different reasons. .... "as you think about this principle, remember that the reasons for change are people."
@baatar
@baatar 4 жыл бұрын
I think it might be worth it for Kyle to make a followup video encompassing these points.
@ikemkrueger
@ikemkrueger 2 жыл бұрын
That's why we have microservices.
@alvoradaprofética
@alvoradaprofética 3 жыл бұрын
Your English is perfect thanks from Brazil 👏
@daninmanchester
@daninmanchester 2 жыл бұрын
Should you not inject your logger, or maybe even use events for looser coupling? Using an interface would allow you to inject a logger of any type and would be helpful for testing. If you are likely to have multiple events, then I would think raising a max calories event would be best as you could hook all manner of handlers to this.
@daminipurohit6916
@daminipurohit6916 4 жыл бұрын
By far the best channel for learning web things! Thank you
@oleksandrvorovchenko8674
@oleksandrvorovchenko8674 5 жыл бұрын
Thanks. And yes, would be great to cover the other patterns.
@ragilburhanudinpamungkas9571
@ragilburhanudinpamungkas9571 4 жыл бұрын
Wold you like to make an express tutorial with typescript that implement SOLID design pattern? Thank you.
@EmanuelCaesar
@EmanuelCaesar 4 жыл бұрын
This is the best solid explanation.... tired of other channels xD
@tajpouria
@tajpouria 5 жыл бұрын
Great job Kyle you should definitely make content about three others principles using multiple examples,, thanks again dude
@netherlabgames7511
@netherlabgames7511 4 жыл бұрын
Congratulations you explained something in 6 min that others can't in 1 hour
@Sina-hr9fv
@Sina-hr9fv 4 жыл бұрын
thank you very much Kyle. you produce great content for free. i really adore you. by the way, what is that settings.json doing there? how are you handling es6 module & cors without a server?
@vimtor
@vimtor 5 жыл бұрын
Hey Kyle. Excellent video as always! A question arise from watching the video: Isn't the CalorieTracker still responsible for logging the surplus? I understand that it does not contain the function for logging itself but it contains the logic of when to print and what message to use. I imagine that in a real world scenario you will use some kind of event system and when notified a CalorieLogger (for lack of a better name) will notice and do whatever it wants to do. What do you think about this? It is worth the effort to think in these terms or you can simply view composition as a way to separate responsibility? Thanks!
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
This is a great point. In most applications generally it is fine to have that type of logic in the calorie tracker class since the main thing you are abstracting is how the logging works. The only time I would worry about event based systems was if I wanted to completely decouple two systems by using events between them to communicate. This is generally only useful in specific situations in larger scale applications since it introduces a bunch of other complexity and indirection related to the event system.
@hanumantuttekar.8316
@hanumantuttekar.8316 5 жыл бұрын
Thank you. Please cover other letters of SOLID Principle 🙏🙏👍
@labwax
@labwax 5 жыл бұрын
Yes, please cover the other letters too. I really like the example.
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
Thanks. I will do.
@ThomasIkemann
@ThomasIkemann Жыл бұрын
Hi Kyle, stupid question. What if I have a class like Car that can drive, stop, turn etc. Would the single responsibility principle "force" me to create own classes for these methods? That does not make sense for me^^ All those methods like toString(), substring() etc. are gathered below the very same class String as well, or do I missunderstand something here? I get the main point though, that a method should have one single reason only, so that if something has to change, you only change one spot of the class instead of one big giant method.
@kerryd2060
@kerryd2060 5 жыл бұрын
My hosting expires on Jan 4th. I'll check out your hosting.
@dz28021406
@dz28021406 2 жыл бұрын
Very good explanation... Well done mate.
@jamstawildman
@jamstawildman 4 жыл бұрын
Hi Kyle. I've only recently discovered your channel. There are others covering similar topics but yours is the best! Super helpful stuff :)
@saroj_kumar
@saroj_kumar 5 жыл бұрын
Hey Kyle. One thing I noticed from these comments that, you responded every comment that says good about the videos. I am a big fan too. Instead I wanted, you must answer the comments that are facing issues/problems running these examples. #Thanks_if_you_consider_this_worth_replying
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
I try to reply to comments asking for help or negative comments as well, but those comments take more time to formulate answers for and I cannot respond to as many.
@saroj_kumar
@saroj_kumar 5 жыл бұрын
@@WebDevSimplified Now I got it! ☺️. One question from my side. I tried to import that function as u said in video, but it says Cannot import module outside the function. I am using npm live-server. Please answer if problem is in not using Node server or what else ?? 🤗
@saroj_kumar
@saroj_kumar 5 жыл бұрын
Anyone else knows the answer can reply.
@ulfdellbrugge4300
@ulfdellbrugge4300 Жыл бұрын
I think it could be improved if the message was just returned or if the message function was dependency injected?
@mohsinalisep
@mohsinalisep 4 жыл бұрын
You did a smooth job explaining it!
@stith_pragya
@stith_pragya Жыл бұрын
Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@imranhaider4316
@imranhaider4316 5 жыл бұрын
Thanks Kyle for making so complex things easy for us.
@Shubham-xh9nz
@Shubham-xh9nz 4 жыл бұрын
Im glad that i found your channel
@Shubham-xh9nz
@Shubham-xh9nz 4 жыл бұрын
keep posting
@A_Lesser_Man
@A_Lesser_Man 4 жыл бұрын
I've been doing this, mostly. I'm glad I now know a name for the idea. Time to start combing through all 30,000+ lines of code. Eek.
@petertester915
@petertester915 5 жыл бұрын
DAS IST GUT!!! More solid please!!
@olyalitvinova9213
@olyalitvinova9213 Жыл бұрын
Hey! does it mean that class better have no more than 1 method?
@edwardtam1456
@edwardtam1456 5 жыл бұрын
This looks good. I think there could be better choice for the example though, because console logging is kind of generic and all over the place that usually people treat it as part of everything.
@ZaidIrfanKhan
@ZaidIrfanKhan 4 жыл бұрын
Hello, I am getting an error: Cannot use import statement outside a module. I guess it's something to do with the settings.json file. What changes would I need to make in that file, in order to make this work in VS code console? Any help would be appreciated
@copyfx00
@copyfx00 3 жыл бұрын
I'm guessing you already solved it, but if someone got this problem, i fix it by adding: type="module" in the script tag of the Html, so now it is: ps: this only work using live-server
@ZaidIrfanKhan
@ZaidIrfanKhan 3 жыл бұрын
@@copyfx00 yup that's how I solved it, thanks for the reply
@cinammondream
@cinammondream 2 жыл бұрын
This is great, thank you!! Amazing video!
@jjnimes
@jjnimes 4 жыл бұрын
Very helpful and well explained! Auto subscribed haha
@julienwickramatunga7338
@julienwickramatunga7338 4 жыл бұрын
Nicely explained!
@ritwizsinha1261
@ritwizsinha1261 4 жыл бұрын
You certainly simplified the web for me
@דורשמואל-ס6ט
@דורשמואל-ס6ט 4 жыл бұрын
You explain so good man
@AslamD
@AslamD 4 жыл бұрын
very helpful video. can we also call this microservice ? or is it different?
@zymzym2455
@zymzym2455 4 жыл бұрын
Can you please share how do you get your code to auto run in the in the Chrome dev console? Thanks
@danilopaulinodasilva
@danilopaulinodasilva 4 жыл бұрын
This is not possible in the "simple" way he show in video. Is not simple as "Click with right click on the mouse and open JS with browser LOL". Here what I do to get this done: 1. Created a file index.html 2. Put the inside this file 3. Open it with Live Server plug-in inside the Visual Studio Code Important: Without the type="module" recent browsers will not understand the import in the code!
@НиколайТодоров-и9т
@НиколайТодоров-и9т 2 жыл бұрын
Very concise and up to the point, bravo!
@reelstrendingsongs
@reelstrendingsongs 4 жыл бұрын
1000 subs/day ( wow congrats )
@nicbongo
@nicbongo 4 жыл бұрын
How would you apply this principal when working with fetch requests and state?
@suryapratapsingh5149
@suryapratapsingh5149 5 жыл бұрын
Super cool and simple way ... God bless you.
@Indrajeetviper
@Indrajeetviper 5 жыл бұрын
Just one thing, why don't you use semi colons at EOS?
@learnbit_bo
@learnbit_bo 3 жыл бұрын
clear explanation thank you
@pastorfred2543
@pastorfred2543 5 жыл бұрын
Great Tutorial.. My Hero Always On Point. Thanks Sir.
@saradamala7652
@saradamala7652 4 жыл бұрын
Which is the best class or composition function?
@Horoe
@Horoe 5 жыл бұрын
Uncaught SyntaxError: Cannot use import statement outside a module
@tannerdolby1361
@tannerdolby1361 4 жыл бұрын
This is because your tag is not expecting you to import a module with Update it:
@uzairsaqib9298
@uzairsaqib9298 3 жыл бұрын
You are great man !!!
@arinmovsesian
@arinmovsesian Жыл бұрын
prefect explanation
@vnm_8945
@vnm_8945 3 жыл бұрын
I want to ask a simple question I just noticed. What's the difference between a function written using the "function" keyword as "function example() {}" and writing it as "example() {}"? Thank you.
@hehimselfishim
@hehimselfishim Жыл бұрын
been two years and i bet youre probably a senior dev by now but if not adding the "function" keyword is using when youre declaring the function, then using it the other way is when its in classes or when youre calling the function.
@RodrigoButta
@RodrigoButta 5 жыл бұрын
Thank you!!! It's nice to refresh all this kind of good practices.
@dmytrodanko8592
@dmytrodanko8592 2 жыл бұрын
ok but it is brake another SOLID principle that said - do not change if it made (open/close principle, you can't change some method if it in use)
@usama57926
@usama57926 5 жыл бұрын
what is expert default function.....❓❓❓❓
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
It is ES6 modules. I have a video on modules you can checkout on my channel for an in depth explanation.
@RN-ru2rj
@RN-ru2rj 4 жыл бұрын
Awesome 👍. Thanks a ton!
@ajithvencode916
@ajithvencode916 4 жыл бұрын
awesome man..keep going
@srinivasgangaraju2998
@srinivasgangaraju2998 Жыл бұрын
video starts @ 1:26
@noice1006
@noice1006 Жыл бұрын
Thank you
@Abhishekkumar-vf8qe
@Abhishekkumar-vf8qe 5 жыл бұрын
Thanks for such a great videos
@redye5858
@redye5858 5 жыл бұрын
love your tuts man
@shamilmammadov8246
@shamilmammadov8246 5 жыл бұрын
Please cover other principles with such simple example
@vossert
@vossert 3 жыл бұрын
Hi Kyle, Thank you so much for all the effort you put into this channel. You taught me so much already! I prefer hands-on learning so I code along with all your videos. What is your VSC setup so you can run the js and see the output on the right side in developer tools / console? Is it a new node project every time?
@bryceblazegamingyt9741
@bryceblazegamingyt9741 Жыл бұрын
I think he's loading an html document in his browser and using that as a test-bench.
@InvincibleMan99
@InvincibleMan99 4 жыл бұрын
Would you mind if you can share code ?
@utkarshdixit7235
@utkarshdixit7235 3 жыл бұрын
Just confused as to why are you not using the sponsor's service for your own website server, if it's more powerful than your current hosting service, on top of 1-year free hosting. I smell something fishy xD Great video btw
@someone-gp8fm
@someone-gp8fm 3 жыл бұрын
Now what about the part where you're checking if the current calories is greater or equal to max calories, shouldn't this get handled by another class too? Because now you are doing to things adding the calories and validating the value, also sometimes having a single class to do a very small task seems to be overkill, what about having other methods in the class and each method takes a single responsibility?
@ahmedboutaraa8771
@ahmedboutaraa8771 4 жыл бұрын
great as allways
@CommunityAllSeasons
@CommunityAllSeasons 5 жыл бұрын
This is amazing content!
@michaborski7383
@michaborski7383 3 жыл бұрын
thank you !
@Michael89312
@Michael89312 Жыл бұрын
very good. cheers
@dangnguyen.92
@dangnguyen.92 5 жыл бұрын
yes, pls do the rest principles
@Atif1702
@Atif1702 4 жыл бұрын
Video starts at 1:26
@michaelNguyen914
@michaelNguyen914 5 жыл бұрын
Amazing channel
@samueltorres2485
@samueltorres2485 3 жыл бұрын
Just found this channel. Great content, you teach these complex concepts well.
@khkh175
@khkh175 4 жыл бұрын
to save time go to 1:36
@lestrae
@lestrae Жыл бұрын
Thanks!
@carpediem73
@carpediem73 4 жыл бұрын
thank you
@gsriraj
@gsriraj 3 жыл бұрын
God bless you man
Open/Closed Principle Explained - SOLID Design Principles
10:22
Web Dev Simplified
Рет қаралды 142 М.
Uncle Bob’s SOLID Principles Made Easy 🍀 - In Python!
19:09
ArjanCodes
Рет қаралды 305 М.
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
The CSS Display Property is Changing Forever
15:20
Web Dev Simplified
Рет қаралды 71 М.
I Was Wrong About Single Responsibility Principle | Prime Reacts
8:14
Liskov Substitution Principle Explained - SOLID Design Principles
10:24
Web Dev Simplified
Рет қаралды 131 М.
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 492 М.
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,8 МЛН
Why I’m Switching To Go in 2024
8:10
Awesome
Рет қаралды 97 М.
Single Responsibility Principle in React (Design Patterns)
16:50
Cosden Solutions
Рет қаралды 51 М.
Solid Programming - No Thanks
32:00
ThePrimeTime
Рет қаралды 348 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН