Skip to main content

JavaScript Example

Violate SRP

// this violates SRP because this class has 2 reasons to change
class CalorieTracker {
constructor(maxCalories) {
this.maxCalories = maxCalories;
this.currentCalories = 0;
}

// 1st reason to change: how we track the calories
trackCalories(calorieCount) {
this.currentCalories += calorieCount;
if (this.currentCalories > this.maxCalories) {
this.logCalorieSurplus();
}
}

// 2nd reason to change: how we log the calories
logCalorieSurplus() {
console.log("Max calories exceeded");
}
}

const calorieTracker = new CalorieTracker(2000);
calorieTracker.trackCalories(500);
calorieTracker.trackCalories(1000);
calorieTracker.trackCalories(700);

Pass SRP

function logMessage(message) {
console.log(message);
}

export default logMessage;
import logMessage from "./logger.js";

class CalorieTracker {
constructor(maxCalories) {
this.maxCalories = maxCalories;
this.currentCalories = 0;
}

trackCalories(calorieCount) {
this.currentCalories += calorieCount;
if (this.currentCalories > this.maxCalories) {
logMessage("Max calories exceeded");
}
}
}

const calorieTracker = new CalorieTracker(2000);
calorieTracker.trackCalories(500);
calorieTracker.trackCalories(1000);
calorieTracker.trackCalories(700);