It is often considered SOLID principles are for OOP only, but it applies to FP as well.
We will first look into OOP examples. Once grasped the basic ideas, we will move on to FP examples. With this comparison, we should be able to see the benefits in applying SOLID principles on FP.
The aim here is to encourage you in writing more FP codes for your future projects, and to let you enjoy the fruits of FP while preventing your codes from violating SOLID principles.
As you may have noticed, the term "FP" is not used here in regular terms.
For FP in our examples means:
- No ES6
class
syntaxes - No
this
keywords - Uses a factory pattern instead of
new
keywords - Uses the prototypal inheritance when extending variants
In another word, it follows the pattern known as Crockford's method.
If I were to only give examples, I could have used Gist. But, I curated into a Github repo so that it should give you a showcase of JSDoc usages.
If you are interested in more JSDoc examples, I have another project in which I have a plenty of JSDoc comments which focuses on describing module relations.
- Single Responsibility Principle (SRP)
• OOP • FP
There should be only one reason to change a module. - Open-Closed Principle (OCP)
• OOP • FP
A module should be open for extension, but should be closed for modification. - Liskov Substitution Principle (LSP)
• OOP • FP
If you substitute a sub-class for the parent, and breaks, then the sub-class violates LCP. - Interface Segragation Principle (ISP)
• OOP • FP
You should not implement interfaces that are not used. - Dependency Inversion Principle (DIP)
• OOP • FP
HIGH-LEVEL modules should not depend on LOW-LEVEL modules.
Bonus:
To generate *.d.ts
files to /types
:
npm run type:generate
To generate JSDoc documents to /jsdoc
:
npm run jsdoc
- core-js
- @babel/cli
- @babel/core
- @babel/preset-env
- babel-loader
- prettier
- eslint
- eslint-config-prettier
- Filters out all the ESLint rules which conflict with Prettier.
- eslint-plugin-prettier
- Orchestrates ESLint and Prettier together.
- @stylistic/eslint-plugin
- New way of setting rules
- jsdoc
- jsdoc-tsimport-plugin
- jsdoc-plugin-intersection
- typescript
- jest
- babel-jest
- ramda
- @types/ramda
npm install --save core-js ramda;
npm install --save-dev @babel/cli @babel/core @babel/preset-env babel-loader \
prettier eslint eslint-config-prettier eslint-plugin-prettier @stylistic/eslint-plugin \
jsdoc jsdoc-tsimport-plugin jsdoc-plugin-intersection \
typescript jest babel-jest;
Dual-licensed under either of the followings.
Choose at your option.
The UNLICENSE (LICENSE.UNLICENSE) MIT license (LICENSE.MIT)