Writing JavaScript according to SOLID

Have any one used the SOLID programming principle (or any of it's parts) while developing JavaScript?

I've just started up on reading on it but can't seem to find anyone that used it for JS. The only part I find easy to implement/use is the "Single responsibility principle".

What I'm looking for is articles or example where these principles are used. And are there any argument's why one shouldn't use some parts?

For example the "Interface segregation principle" says that "the notion that many client specific interfaces are better than one general purpose interface."

But from my knowledge there's no such thing as interfaces in JS (nice if it would be).


Solution 1:

It looks like Derek Greer is attempting to take a stab at this with his article series on SOLID JavaScript at Fresh Brewed Code:

  1. The Single Responsibility Principle
  2. The Open/Closed Principle
  3. The Liskov Substitution Principle
  4. The Interface Segregation Principle
  5. The Dependency Inversion Principle

Solution 2:

JavaScript sometimes gets a bad rap as being sub-par to those such as C++, C#, and Java, when in fact it's a very powerful functional programming language that also has object oriented capabilities (although it's not really classified as object oriented)

Perhaps many developers look down on it because so many of them are used to seeing poor programming practices and consequently, buggy behavior in JavaScript. For some unknown reason, it seems more acceptable to be sloppy on the client side. This is something I would like to change.

I believe these SOLID principles are solid. (No pun intended). If you follow these conventions, you will be less likely to accumulate technical debt created by sloppy code, shortcuts, and no architecture. Your code will be more maintainable, more reusable, more modular, less tightly coupled, and scalable and extensible. You'll also be contributing to demonstrating the full power of JavaScript when your product is engineered instead of just recklessly slapped together.

This document describes the fundamentals of SOLID. The same rules apply whether you're referring to C++, Java, JavaScript, or any other object-oriented language.

Code Project - The SOLID Object Oriented Programming Principles

Here is some more information on JavaScript concepts on colourcoding.net.

Solution 3:

This accepted answer is flawed. I recommend to read the five articles linked to by Ryan Rensford below. The last article comes to the following conclusion which I failed to communicate (emphasis by me):

While in the course of our examination we saw variations in how the SOLID design principles apply to JavaScript over other languages, each of the principles were shown to have some degree of applicability within JavaScript development.

SOLID is meant for object-oriented programming. JavaScript is a prototype-based language but allows programming in an OOP-manner (if you really try hard to do so). Many people believe that you shouldn't try to force paradigms of languages you learned (like C++/C#/Java) onto others (JavaScript). Here's an article on OOP in JS which also comes to that conclusion.

There are some approaches to OOP in Prototype.js, CoffeeScript, and John Resigs Simple JavaScript Inheritance (each with its own traps).

But the terminology (interfaces, abstraction) of SOLID is difficult to apply to vanilla JavaScript in a proper manner. You will be able to apply "S", and maybe the "L" (which are good concepts). But going further would require constructs like interfaces (which are hard to find in dynamic languages anyway, contracts might work) and the ability to restrict inheritance/modification.