Does web design need education?
The profession of web designer is now becoming one of the most sought after. Every month, thousands of new people with burning eyes appear who think that this is their…

Continue reading →

The history of web design: from the Stone Age to the era of modern technology
Despite the fact that in the era of the development of web design, the first web pages were just text with links, and you could only dream of a variety…

Continue reading →

Features of the profession and key responsibilities of a web designer
To make it easier to understand the main tasks of the representative of this craft, for a start it is worth considering its basic definition. The profession of "web designer"…

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.

Effective web design - what is it?
If you want to achieve serious heights and build a successful career as a web designer, then you definitely need to know the key criteria for evaluating the effectiveness of…

...

Web Layout 101: How to Get the Most Out of the Top Right
From the author: studies of user behavior showed that the upper right corner of the site attracts the least attention. This is a common reflection of our reading habits. We…

...

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…

...

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…

...