How to become a web designer, and what qualities should a really cool specialist have?
From the author: many of you were probably looking for the answer to the question: how to become a web designer? Of course, this is a difficult question, to which…

Continue reading →

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…

Continue reading →

Minimalistic website design: features, advantages and disadvantages
In the past few years, web design has alternately reigned in a flat style, now a skeuomorphism, now a material one, then again a flat one ... All of these…

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 ({

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…

...

Custom CSS Properties
A Ashley Nolan poll in November 2016 showed that 84% of front-end developers use a CSS preprocessor. At the same time, developers consider variables to be the most useful preprocessor…

...

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…

...

Web Design Rules Every Professional Should Know
The basic principles are the same for each representative of this field of activity. They are followed by both experienced professionals and even green beginners. Do you think that, having…

...