BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

SOLID Design Principles for JavaScript

by Jan Stenberg on Jan 22, 2014 |

Many developers have worked in object oriented languages and many are working in JavaScript but very few use object orientation principles together with JavaScript, Derick Bailey, an author and developer focusing on JavaScript, stated in a recent presentation at CodeMash. In object oriented programming we talk about foundations and principles as a base for our work but when moving from class based static languages to loosely typed, not class based languages, we often find it hard to apply the same principles.
Derick claims there are a lot of good principles, practices and patterns available in order to help developers writing good stable JavaScript code, one example being the SOLID principles, identified by Robert C. Martin, in the early 2000s.
Derick describes the SOLID principles as five individual patterns that play well together and walks through all five using code samples and looking at some idiosyncrasies in JavaScript that makes applying these principles a little bit different compared to when using them in languages like Java and C#.
Derick’s definition of the five principles are:

  • Single Responsibility Principle. Everything should have only one reason to change. This will help developers understand the context and responsibility of what they are building and when there is a need for change.
  • Open-Closed Principle. A change in behaviour should be possible without changing existing code, e.g. by using extension points and creating code that can be plugged in.
  • Liskov Substitution Principle. Derived objects or types must be substitutable for their base. For Derick this is a more focused version of the Open-Closed principle.
  • Interface Segregation Principle. A client should not be forced to depend on interfaces it doesn’t use. A problem is that there are no explicit interfaces in JS, but there are ways around this.
  • Dependency Inversion Principle. Consists of two concepts, abstraction which states that we should depend on abstractions, not on concrete implementations and ownership that states that low level implementation should depend on high level concepts.

Derick ends with stating that if you have large monolithic chunks of code in your system, SOLID will help you break these into individual pieces. It will not decrease complexity, but will help you create abstractions and group details into larger concepts that we can reason about.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT