visit
SOLID is just a set of design principles that all coders and developers should follow to achieve a good design in the software they build. None other than Robert C Martin proposed it. SOLID principles explain how to arrange our functions and data structures and how they can be interconnected.
The main goal of the SOLID principles is that any software should tolerate change and should be easy to understand. If we desire to build quality software, the SOLID principle is essential to be followed.Most people assume SOLID is only for strongly typed languages or object-oriented languages like JAVA and more. Though Javascript is a loosely typed language, we can easily apply SOLID principles on Javascript code. Let’s see how we can do that in this article.
function createUser(name, email){
let re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
let isEmailValid = re.test(email);
if(isEmailValid){
createUserInDabase(name, email)
}
}
function validateRequest(req){
let re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
let isEmailValid = re.test(req.email);
if(isEmailValid){
createUser(req.name, req.email)
}
}
function createUser(name, email){
createUserInDabase(name, email)
}
let allowedRoles = ["admin", "owner"]
function checkPrivilege(user){
if(allowedRoles.includes(user.role)){
return true; //user has privilege
}else{
return false
}
}
Now, what if the software system introduces a new role called guestAdmin and users with guestAdmin roles should also be granted privilege. So here we have to modify existing code to add guestAdmin to the list. So rather we can do like the below example to make it pass the Open-Closed principle.
let allowedRoles = ["admin", "owner"]
function checkPrivilege(user){
if(allowedRoles.includes(user.role)){
return true; //user has privilege
}else{
return false
}
}
addRoles(role){
allowedRoles.push(role)
}
As per the LSP, functions that use references to base classes must be able to use objects of the derived class without knowing it. In simple words, derived classes must be substitutable for the base class. Let’s see the Liskov Substitution Principle with an example:
var License = function(user){
this.calculateFee = function (){
//Logic to calculate Fee
}
}
License.prototype.PersonalLicense = function(user){
this.calculateFee(user)
}
License.prototype.BusinessLicense = function(user){
this.calculateFee(user)
this.getUsers = function(){
//Logic to get all users in Business
}
}
If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.
class User{
constructor(user){
this.user = user;
this.initiateUser();
}
initiateUser(){
this.name = this.user.name
this.validateUser()
}
}
const user = new User({userProperties, validateUser(){}});
class User{
constructor(user){
this.user = user;
this.initiateUser();
this.setupOptions = user.options
}
initiateUser(){
this.name = this.user.name
this.setupOptions()
}
}
const user = new User({userProperties, options: {validateUser()}{}});
axios.get("/someAddress/someResource", function (response) {
this.setState({
value1: response.value1,
value2: response.value2
});
});
requestWithAxios("/someAddress/someResource", setResponseInState);
function requestWithAxios(address, setResponseInState){
axios.get("/someAddress/someResource", function (response) {
setResponseInState.setValues(response);
});
}
var setResponseInState ={
setValues: function(response){
this.setState({
value1: response.value1,
value2: response.value2
})
}
}
I head Tech at Upshotly. We are excited about building tools for modern leaders to help them put their people at the heart of business success. If you think you benefited from this blog, please share it with your friends and co-workers! In case you have any queries, clarification, or an idea for my next blog, please let me know in the comments!