DEV Community

Cover image for Solid Principles in Javascript

Solid Principles in Javascript

Francesco Ciulla on March 18, 2020

The SOLID principles are a set of software design principles, that help us to understand how we can structure our code in order to be robust, maint...
Collapse
 
paulosandsten profile image
Paulo Sandsten • Edited

When reading about the SRP, I see a conflict in how it is being described here versus how uncle Bob describes it in his book, "Clean Architecture"

"Of all the SOLID principles, the Single Responsibility Principle (SRP) might be the least well understood. That's likely because it has a particular inappropriate name. It is too easy for programmers to hear the name and then assume that it means that every module should do just one thing.

Make no mistake, there is a principle like that. A function should do one, and only one, thing. We use that principle when we are refactoring large functions into smaller functions; we use it at the lowest levels. But is is not one of the SOLID principles – it is not the SRP" - Uncle Bob

In the book mentioned, Uncle Bob defines it:
"A module should be responsible to one, and only one, actor"

With an actor, he mentions that it refers to one or more people who requires a change.

I really like that you are writing about SOLID and sharing. Thank you for that. I would like to hear more about your reasoning here regarding SRP.

Cheers.

Collapse
 
dimpiax profile image
Dmytro Pylypenko • Edited

Hey, in your example of S, good to see clear example of responsibilities conjunction, where you follow also D principle:

// process
validateRequest = (req) => isValidForm(req) && createUser(req)

//Call an external function to validate the user form
isValidForm = (req) => testForm(req.name, req.password, req.email)

//Only Create User in the Database
createUser = (req) => User.Create(req.name, req.password, req.email)

What is the purpose of example in I:

initiateUser() {
  this.username = this.username; // this?
  this.password = this.password; // this?
  this.validateUser(this.username, this.password);
}
Collapse
 
francescoxx profile image
Francesco Ciulla

Dear Dmytro,
Thank you for noticing, the example in "I" has been revised.
But the intent was to just show the purpose of the principle, not the concrete implementation, which is lacking of course! As the validation method is just loggining a string :)

Collapse
 
jochemstoel profile image
Jochem Stoel

Hey, this was nice to read. Good for my ego. Did you come up with this SOLID JavaScript or is this a common term on the web?

Collapse
 
kopseng profile image
Carl-Erik Kopseng • Edited

It's a 20 year old concept made popular by "Dr. Bob" (i.e. the agile programming disciple Robert Martin of "Clean Code" fame) that was very widely disseminated in the first decade of the so called "software craftsmanship" movement. en.wikipedia.org/wiki/SOLID

Collapse
 
francescoxx profile image
Francesco Ciulla

A fairly widespread concept. Thanks for the feedback

Collapse
 
jochemstoel profile image
Jochem Stoel

I had never heard of this SOLID thing but arrived at exactly the same conclusions and methods myself so that is always nice to discover.

Collapse
 
freshi profile image
Wachirajob

Nice post with good examples.

Collapse
 
francescoxx profile image
Francesco Ciulla

Thank you

Collapse
 
damxipo profile image
Damian Cipolat

Nice examples, but I think the "L" of lisvok is useless if you don't work in JS using OOP.

Collapse
 
francescoxx profile image
Francesco Ciulla

Thank you. The important part is to grasp the concept.