SOLID Principles in JavaScript
SOLID Principles in JavaScript
FOLLOW
Sina Riyahi
--- Bad Code
cLass HttpRequester {
constructor(adapter) {
th-Ls.adapter = adapter;
}
fetch(urL) {
i._f (th-Ls.adapter . name "ajaxAdapter") {
return makeAjaxCaLL(UrL).then(response => {
// t,-ansfo,-m ,-esponse and ,-etu,-n
} );
} eLse i._f (th-Ls.adapter.name === "nodeAdapter") {
return makeHttpCaLL(UrL).then(response => {
// transfor-rn ,-esponse and ,-etur-n
} );
}
}
}
function makeAjaxCaLL(UrL) {
// request and return promise
}
function makeHttpCaLL(UrL) {
// request and return promise
}
FOLLOW
Sina Riyahi
---
CLass RectangLe
constructor()
{
{
Bad Code
Liskov Substitution Principle (LSP)
0
th"i..s .wi.dth ©;
th"i..s.hei.ght = ©;
}-
setCoL□r(c□L□r) {
//
}-
render(area) {
//
}-
setWi.dth(wi.dth) {
th"i..s .wi.dth = wi.dth;
}-
setHei.ght(hei.ght {
th"i..s .hei.ght hei.ght;
}-
getArea( {
return thi.s .wi.dth * thi.s .hei.ght;
}-
}-
setHei.ght(hei.ght) {
th"i..s.wi.dth hei.ght;
thi.s .hei.ght hei.ght;
}-
}-
functi.on renderLargeRectangLes(rectangLes) {
rectangLes.forEach(rectangLe => {
rectangLe. setWi.dth(4);
rectangLe. setHei.ght(S);
canst area = rectang�e.getArea( ); // BAD: Returns 25
for Square . ShOULd be 20 .
rectangLe.render(area);
}- ) ;
}-
FOLLOW
Interface Segregation Principle (ISP)
FOLLOW
Sina Riyahi
Dependency Inversion Principle (DIP)