Do frontender need algorithms and design patterns?
In fact, you probably already use them, but you can even better.
The algorithm is frightening many people; it seems to be something complicated, but in reality it’s just a complete set of instructions. It turns out that you use algorithms in everyday life, for example, when you are cooking with a recipe, or you use the navigator from point A to point B, or solve a quadratic equation.
When developers talk about algorithms, they do not mean all algorithms, but only popular solutions of standard problems. Many algorithms were invented before computers: for example, a bitwise sorting algorithm was patented in the USA in the nineteenth century for processing data obtained after a population census.
Different algorithms may be suitable for solving the same problem. Imagine you have a list in which to find an item. Suppose that this is a list of products in the online store and the user enters the name of the product in the filter, which begins with the letter "E". How to do it?
If the list is sorted alphabetically, a binary search is suitable for you - you look in the middle of the list, find there a product whose name begins, for example, in “K”. The list is sorted, so you know for sure that the product you need is in your left-hand part of the list, because the “E” in the alphabet is earlier than the “K”. Now you take the left side of the list and repeat the same procedure with it.
If the list is not sorted, brute-force is better suited - you go in order from the beginning of the list to the end of it and try to find the element that interests you. In the worst case, you will have to look at all the elements, but then you will know in advance the time you spend on finding the right item.
You need to choose an algorithm for the task. Understand what data you are working with and build on it. There are algorithms for working with lists, arrays, trees, and so on. To choose an algorithm, you need to understand not only its advantages, but also its disadvantages: they are also well known and described. For example, there is a website that helps you choose the right sorting algorithm, visually showing a comparison of the work of different algorithms on different lists.
Now let's talk about design patterns. Patterns are simply stable constructs in programming. All programmers in the world solve more or less similar tasks and popular solutions have already been developed to solve these problems. There is a famous book on this topic - the book “Gangs of Four” , which is called “Design Patterns”. There are also two excellent books from Eddie Osmani:
Patterns help save time on organizing code and focus on solving a problem. There are patterns that tell you how to write some kind of individual solutions in the code, for example the “enumeration” pattern; there are patterns that describe how best to divide the application code into folders and what to write in specific files, for example the MVC pattern and its relatives MVP and MVVM; and there are patterns that tell how different modules should communicate with each other, for example, the pattern “inversion of control”.
It may seem that in order to apply patterns, you need to understand programming very deeply and work on complex tasks, but in reality this is not always the case: developers often use patterns without even knowing it, because some patterns are built right into programming languages .
Imagine that you have a button on your site that a user clicks on. And there is an algorithm that you want to run when the user clicks on this button. To link them together, an “event handler” is used - the implementation of the “observer” pattern, which is embedded in JavaScript right at the language level.
var firstModule = function() { console.log('Hi'); }; button.onclick = function(e) { firstModule(); };
So, does the frontendor need to learn algorithms and patterns? Yes, you still encounter them in your work every day, and if you do it consciously, you will be able to solve your problems more effectively.
Questions can be asked here .
Source: https://habr.com/ru/post/342054/
All Articles