Landing design: how to make a capture page
What is a landing page? The concept of landing page, as, in principle, the landing design, came to us relatively recently, according to the official version from the USA, 10-12…

Continue reading →

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…

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 →

SOLID principles in JavaScript. Open-closed principle

The essence of the principle
The principle of openness-closeness is as follows:

“Software entities (classes, modules, functions, etc.) must be open for expansion, but closed for change.”

That is, the application architecture should allow changing the behavior of certain entities without changing their source code.

This principle is especially important for large business applications, when requirements change, functionality expands or changes, which can lead to a lot of unexpected changes. A cascade of changes entails, in turn, labor costs for testing and unpleasant unpredictable errors.

JavaScript open-close principle
To illustrate the principle of openness-closedness in JavaScript, consider an example that implements a simple survey:

Javascript
/ **
* Types of questions (an object containing possible types of questions)
* /
var AnswerType = {
Choice: 0,
Input: 1
};

/ **
* Question
* /
function question (label, answerType, choices) {
return {
label: label,
answerType: answerType,
choices: choices
};
}

/ **
* Presentation object
* /
var view = (function () {
/ **
* Function display one question
* /
function renderQuestion (target, question) {
var wrapper = $ (‘<div class = “question”> </div>’),
label = $ (‘<div class = “question-label”> </div>’),
answer = $ (‘<div class = “question-input”> </div>’);

label.html (question.label);

if (question.answerType === AnswerType.Choice) {
var input = $ (‘<select> </select>’);
var len = question.choices.length;
for (var i = 0; i <len; i ++) {
var option = $ (‘<option> </option>’);
option.text (question.choices [i]);
option.val (question.choices [i]);
input.append (option);
}
}
else if (question.answerType === AnswerType.Input) {
var input = $ (‘<input type = “text” />’);
}

answer.append (input);
wrapper.append (label);
wrapper.append (answer);
target.append (wrapper);
}

return {
/ **
* Question list display function
* /
render: function (target, questions) {
for (var i = 0; i <questions.length; i ++) {
renderQuestion (target, questions [i]);
};
}
};
}) ();

var questions = [
question (‘Your age’, AnswerType.Input),
question (‘Do you like science fiction?’, AnswerType.Choice, [‘Yes’, ‘No’]),
question (‘How many hours per week do you spend on average reading fiction?’, AnswerType.Choice, [‘less than an hour’, ‘2-5 hours’,’ 5-8 hours’, ‘9-15 hours’,’ more than 15 hours’])
];

var questionContainer = $ (‘# questions’);
view.render (questionContainer, questions);
In the example above, the view object has a render () method, which takes a container object and an array of questions and displays each question in turn. Everything seems to be logical, however, adding a new type of question requires changing the render () method, which contradicts the principle of openness-closeness.

Now consider another example where this point is fixed:

Javascript
/ **
* Abstract object displaying a question
* /
function questionCreator (params) {
var that = {};

that.renderInput = function () {
throw “Not implemented”;
};

that.render = function (target) {
var wrapper = $ (‘<div class = “question”> </div>’),
label = $ (‘<div class = “question-label”> </div>’),
answer

label.html (params.label);
answer = that.renderInput ();

wrapper.append (label);
wrapper.append (answer);

return wrapper;
};

return that;
}

/ **
* The object that displays the question with the type of answer is a drop-down list
* /
function choiceQuestionCreator (params) {
var that = questionCreator (params);

that.renderInput = function () {
var input = $ (‘<select> </select>’);
var len = params.choices.length;
for (var i = 0; i <len; i ++) {
var option = $ (‘<option> </option>’);
option.text (params.choices [i]);
option.val (params.choices [i]);
input.append (option);
}

return input;
};

return that;
}

/ **
* An object displaying a question with an answer type – input field
* /
function inputQuestionCreator (params) {
var that = questionCreator (params);

that.renderInput = function () {
var input = $ (‘<input type = “text” />’);
return input;
};

return that;
}

var view = {
render: function (target, questions) {
for (var i = 0; i <questions.length; i ++) {
target.append (questions [i] .render ());
}
}
};

var questions = [
inputQuestionCreator ({
label: ‘Your age’
}),
choiceQuestionCreator ({
label: ‘Do you like science fiction?’,
choices: [‘Yes’, ‘No’]
}),
choiceQuestionCreator ({

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…

...

Blog Design: Layout Workshop
The topic of today's post is blog design. We will have a practical lesson, so get ready for independent work, there is no planned vacation :) Why is the master…

...

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…

...

Flat Design Sites (Flat Design): history, principles, examples for inspiration
What do you think is the reason for such a frenzied popularity of flat design? I will answer you in a nutshell: it really works! In this article, I will…

...