Комментарии:
Whenever I see this guys videos I have two thoughts:
1. He's a good teacher/explainer...
2. How the hell does he get his hair so perfect looking?
class CalorieTracker {
constructor(maxCalories) {
this.maxCalories = maxCalories;
this.currentCalories = 0;
this.calorieLogger = new CalorieLogger();
}
trackCalories(calorieCount) {
this.currentCalories += calorieCount;
if (this.currentCalories > this.maxCalories) {
this.calorieLogger.logCalorieSurplus(this.currentCalories, this.maxCalories);
}
}
}
class CalorieLogger {
logCalorieSurplus(currentCalories, maxCalories) {
console.log(`You went over ${currentCalories - maxCalories} calories!`);
}
}
let tracker = new CalorieTracker(2000);
tracker.trackCalories(500);
tracker.trackCalories(1000);
tracker.trackCalories(1500);
==============================================================================
// main.js
import logCalorieSurplus from './logger.js';
class CalorieTracker {
constructor(maxCalories) {
this.maxCalories = maxCalories;
this.currentCalories = 0;
this.calorieLogger = logCalorieSurplus;
}
trackCalories(calorieCount) {
this.currentCalories += calorieCount;
if (this.currentCalories > this.maxCalories) {
this.calorieLogger(this.currentCalories, this.maxCalories);
}
}
}
let tracker = new CalorieTracker(2000);
tracker.trackCalories(500);
tracker.trackCalories(1000);
tracker.trackCalories(1500);
// logger.js
export default function logCalorieSurplus(currentCalories, maxCalories) {
console.log(`You went over ${currentCalories - maxCalories} calories!`);
}
==============================================================================
Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
ОтветитьThanks!
ОтветитьHey! does it mean that class better have no more than 1 method?
Ответитьi think this violates the yangni principle
Ответить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.
prefect explanation
Ответитьthanks
ОтветитьDo you even blink bro?
Ответитьvery good. cheers
ОтветитьI think it could be improved if the message was just returned or if the message function was dependency injected?
ОтветитьThis is great, thank you!! Amazing video!
Ответитьof course we want
ОтветитьDoes this imply that a class can only have one method? It doesn't sound right.
Ответить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)
Ответитьyou literally have a video for eveything! Thank you so much!
Ответить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).
Ответить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 ?
ОтветитьVery concise and up to the point, bravo!
Ответить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
Ответить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.
Good explanation. However, IMO to really separate responsibilities, CalorieTracker should call a notifyCaloriesExceeded function and that function would contain the message to output/email.
ОтветитьVery good explanation... Well done mate.
ОтветитьSRP = High cohesion and low coupling
Ответитьamazing tutorials, thank you
pd. please blink
Your English is perfect thanks from Brazil 👏
ОтветитьVery well explained !!!
Ответить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?
ОтветитьFinally! I understand the S in SOLID. Now for the rest of the acronym :P
ОтветитьGod bless you man
ОтветитьYou are great man !!!
Ответить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
thank you !
Ответить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.
ОтветитьAnother way to say it: if you don't see a "this" in it, then it should be outside of the class
ОтветитьYour videos are awesome, but unfortunately, you misunderstood Single Responsibility Principal, like most people. Please read Uncle Bob's Clean Architecture book.
Ответить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.
Just found this channel. Great content, you teach these complex concepts well.
Ответить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.
ОтветитьIt doesn't makes sense. If I want to change both - the message and the format of the message i need to change it in two places.
Ответить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?
Ahh, a cousin to the MVC Pattern 🧐
Ответитьclear explanation thank you
ОтветитьNicely explained!
Ответить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
ОтветитьWold you like to make an express tutorial with typescript that implement SOLID design pattern? Thank you.
Ответить