Composition in web design, or what Photoshop tutorials are silent about
Surely, you often have to visit sites that, at first glance, seem to be like nothing - both the pictures are beautiful and the text is folding, however, they lack…

Continue reading →

Visual Regression Testing with PhantomCSS
If you worked on large, serious projects, you probably already felt the importance of automated testing during the development process. Depending on experience, this awareness may come suddenly like a…

Continue reading →

What is $ .noConflict ()?
jQuery uses the $ character as a shorthand for jQuery. Many other libraries also use this symbol. So, connecting several of these libraries may lead to the fact that some…

Continue reading →

SOLID principles in JavaScript. The substitution principle of Barbara Liskov

The substitution principle is most often used in the context of inheritance, however, the essence of the principle is more connected not with inheritance, but with the behavioral compatibility of objects. This refinement is especially relevant for JavaScript, as for a dynamically typed language.

In object-oriented design, inheritance provides a mechanism for reusing code in similar classes. This is achieved by encapsulating general behavior in the base class and overriding / extending behavior in the heirs. According to the principle of substituting Barbara Liskov, the behavior of the heir should be equivalent to the behavior of the base class.

An example of a violation of the principle of substitution Barbara Liskov
Consider the following example:

Javascript
function Vehicle () {
var that = {
speedValue: 0,
running: false
};

that.speed = function () {
return that.speedValue;
};
that.start = function () {
that.running = true;
};
that.stop = function () {
that.running = false;
};
that.accelerate = function () {
that.speedValue ++;
};
that.decelerate = function () {
that.speedValue–;
};
that.state = function () {
if (! that.running) {
return ‘parked’;
}
else if (that.running && that.speedValue) {
return ‘moving’;
}
else if (that.running) {
return ‘idle’;
}
};

return that;
}
An example is a Vehicle object that describes the behavior of a vehicle. Imagine that this object is included in a certain library, which is used in third-party applications. Add a new object that describes a fast vehicle:

Javascript
function FastVehicle () {
var that = new Vehicle ();
that.accelerate = function () {
that.speedValue + = 3;
};

return that;
}
The FastVehicle object inherits from Vehicle, inherits all methods of the base object, and accelerates three times faster. We are publishing a new version of the library. However, FastVehicle objects may not be used in all applications using our library. Consider an example:

Javascript
var maneuver = function (vehicle) {
console.log (vehicle.state ());
vehicle.start ();
console.log (vehicle.state ());
vehicle.accelerate ();
console.log (vehicle.state ());
console.log (vehicle.speed ());
vehicle.decelerate ();
console.log (vehicle.speed ());
if (vehicle.state ()! = ‘idle’) {
throw ‘The vehicle is still moving!’;
}
vehicle.stop ();
console.log (vehicle.state ());
};
If we pass the FastVehicle object to the maneuver () function, we get the exception “The vehicle is still moving!”. This happened because the developer of this function proceeded from the fact that vehicles accelerate and decelerate equally. Vehicle and FastVehicle objects are not completely interchangeable, and this violates the Lisk substitution principle.

Classic Rectangle Example
The classic example that is given when describing the principle of substitution is an example with a rectangle and a square, described in a book by Robert Martin.

Imagine that we have a rectangle object:

Javascript
var rectangle = {
length: 0,
width: 0
};
Later we needed a square object. Based on the fact that the square is a rectangle whose sides are equal, we decided to create a square object and use it instead of a rectangle. The length and width properties are defined so that they change synchronously and the square property is not violated:

Javascript
var square = {};
(function () {
var length = 0, width = 0;
Object.defineProperty (square, ‘length’, {
get: function () {return length; },
set: function (value) {length = width = value; }
});
Object.defineProperty (square, ‘width’, {
get: function () {return width; },
set: function (value) {length = width = value; }
});
}) ();
The problem is that the square object cannot be used in any code instead of a rectangle. For example, a function that creates a rectangle and calculates the area:

Javascript
var g = function (rectangle) {
rectangle.length = 3;
rectangle.width = 4;
console.log (rectangle.length);
console.log (rectangle.width);
console.log (rectangle.length * rectangle.width);
};
If we pass an object a square to this function, we get an area equal to 16, instead of the expected 12. Here we also see a violation of the substitution principle, but it is already not connected with inheritance, but with the replacement of one object with another.

Decrease in probability of substitution violation
How to identify and eliminate violations of the principle of substitution Barbara Liskov? Unfortunately, this is a very difficult and not always solvable task. To identify such violations, you need to submit all possible options for using the library in client applications. However, there are several strategies to reduce the likelihood of such violations.

JQuery preloader
There is a delay when loading the page content asynchronously or sending a request to add / edit. A preloader is an animation during this delay, which allows you to…

...

Rubber web design. What and where are we going to stretch?
When developing a website, the webmaster, first of all, should be based on the goals of the project. If it is important for the customer that the web page can…

...

Basic web design elements. How to create an effective website thanks to them?
For the construction of any building requires certain building materials. As a rule, for each type of structure they constantly remain the same, only the shape and general design of…

...

Button State Design
From the author: button design is the main element of any design system. The best of the buttons are simple, versatile, perhaps even a little fun. But there are many…

...