How to start making money as a web designer on the Internet?
From the author: hello friends! Web design training, like any other profession, is gradual. At first it’s just a hobby, which then under certain circumstances can turn into the meaning…

Continue reading →

Web Design 2016: Chrome attacks!
From the author: The year 2016 has ended, it is always interesting to go back and see how much the workflow has improved over the previous 12 months. It's amazing…

Continue reading →

5 signs of failed logos
From the author: you can not minimize the importance of the logo. The logo gives your business a distinctive mark. Branding guru David Brier reminds us all that “one of…

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.

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…

...

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…

...

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…

...