Clean Code Basics: SRP - The Single Responsibility Principle
Let's explore one of the clean code basic principle, named SRP: Single Responsibility Principle, and why it does matters!
Welcome again, brave coder đšâđ». Today, weâll be exploring a new pattern in the Clean Code Land! The Single Responsibility Principle (SRP). Think of it like a game where every character (code) must stick to just one job. Letâs dive in and find out how sticking to SRP will help us level up our code đ
Ala Barakati Lah!
âWhat Is SRP?
Imagine a hero living in castle, and he has multiple jobs: fighting dragons đ cooking meals for the king, repairing the castle, and babysits the kingdomâs kids đ¶ LOL. Thatâs chaos, right đ? Instead, letâs give each hero one job so they can do it efficiently.
In coding, SRP means each function, class, or module should have a single, well-defined responsibility. Your code is like when the hero is doing too much, he will be doing nothing efficiently at the end. So please, donât let your code do too much!
âThe Problem of Multitasking Heroes (codes)
Hereâs a good (messy, haha) example.
Meet our multitasking function manageKingdom:
function manageKingdom(kingdom) {
// Manage finances
kingdom.gold -= 100;
console.log("Gold spent on new armor!");
// Train soldiers
kingdom.army.forEach(soldier => soldier.train());
console.log("Soldiers are ready for battle!");
// Write kingdom laws
kingdom.laws.push("Don't pet dragons!");
console.log("New law added!");
}Whatâs wrong here? đ€
This function is doing too much! Finances, training soldiers, and writing kingdom laws?! Thatâs three jobs in one. Debugging this will be a burden!
â
The SRP Solution!
Now, letâs split this into smaller, single-responsibility heroes:
function manageFinances(kingdom) {
kingdom.gold -= 100;
console.log("Gold spent on new armor!");
}
function trainArmy(kingdom) {
kingdom.army.forEach(soldier => soldier.train());
console.log("Soldiers are ready for battle!");
}
function writeLaw(kingdom, newLaw) {
kingdom.laws.push(newLaw);
console.log(`New law added: ${newLaw}`);
}Now, you can assemble them when needed:
function manageKingdom(kingdom) {
manageFinances(kingdom);
trainArmy(kingdom);
writeLaw(kingdom, "Don't pet dragons!");
}Whatâs better about this?
Each function does one thing.
Debugging is easier, if laws are broken for example, you check only the function
writeLaw.Code is reusable. Need to train soldiers again? Call
trainArmywithout messing with finances.
đWhy SRP Matters?
By following SRP, you unlock these perks:
Clarity: Each piece of code is easy to understand.
Flexibility: Want to change how you train soldiers? No need to touch the finances.
Bug Reduction: Focused heroes mean fewer chances of things breaking.
đ€Wrap up!
Stick to SRP, and your code will be:
Well-organized.
Easy to manage, extend, and debug.
Less likely to turn into a mess!
So next time you code, ask yourself: âAm I making a multitasking hero?â If yes, break it down! From now on, I want you to code responsibly!
Thatâs it for todayâs article, see you soon!
đ Don't forget to subscribe to my blog and YouTube channel to stay updated with all the latest content!





