SOLID PRINCIPLES IN JAVA SCRIPT
SOLID PRINCIPLES IN JAVA SCRIPT
- who invented the solid principal?
- In the year of 2000 Software engineer Robert C.Martin (Uncle bob)Five key design principalin his paper "Design Principesl&Design Patterns"
- The principal aimed to improve Object-Oriented Software Design and reduce code rot.
In year of 2004 Micheal Feathers coined the acronym SOLID make the 5 Principles eaiser to remember and tech.Since then,SOLID has became a cornerstone of clean code and algile development.
What are all the SOLID PRINCIPLES?
- Each letter in SOLID stands for the principle.
- Lets break them down with examples:
A clean function should have only one reason to change.
ex:function saveUser(user){
saveToDB(user);
}
function notifyUser(user){
sendWelcomeEmail(user);
}
Why it matters:Eaiser to debug test and update without side effects.
Open/Close Principles(OCP)
Software should be open for extension but closed for modification .
ex:class Discount {
getDiscount (price) {
return price;
}
}
class StudentDiscount extends Discount{
getDiscount (price){
return price *0.9;
}
}
Why it matters:You can add a new behavior without changing a existing code.
Liskov Substitution Principles (LSP)
Subclasses should be replaceable for their parent classes without breaking the app.
ex:class Bird{
move() {}
}
class FlyingBird extends Bird{
move(){
walk();
}
Why it matters:Prevent unexpected behavior when using subclasses.
Interface Segregation Principles(ISP)
Don't force the classes to implement methods they don't use.
ex:class Printer{
print() {}
}
class Scanner{
scan() {}
}
//Separate Interfaces
Why it matters: Keeps code modular and avoid bloated classes.
Dependency Inversion Principles(DIP)
High- level moduleds should depand on abstractions,not low-level details.
ex:class EmailService{
send(email) {}
}
class User{
constructor (emailService){
this.emailService=emailService;
}
notify(){
this.emailService.send("Welcome");
}
}
Why it matters: Make your code flexible and Testabale.

Comments
Post a Comment