<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Paweł</title>
    <description>The latest articles on DEV Community by Paweł (@kendyl93).</description>
    <link>https://dev.to/kendyl93</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F175069%2Faf376d26-238b-41d9-adcf-e5e2b7ac59b5.jpeg</url>
      <title>DEV Community: Paweł</title>
      <link>https://dev.to/kendyl93</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kendyl93"/>
    <language>en</language>
    <item>
      <title>The Big O notation - O(n)</title>
      <dc:creator>Paweł</dc:creator>
      <pubDate>Sun, 29 Sep 2019 18:20:17 +0000</pubDate>
      <link>https://dev.to/kendyl93/the-big-o-notation-o-n-4b06</link>
      <guid>https://dev.to/kendyl93/the-big-o-notation-o-n-4b06</guid>
      <description>&lt;h2&gt;
  
  
  1. Why I've chosen to write this article?
&lt;/h2&gt;

&lt;p&gt;I have found the Big O notation could be scary for many beginner programmers (&lt;em&gt;including me some time ago&lt;/em&gt;). The subject is really simple and it is worth to dedicate few minutes to read this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. What is this notation and why is it important?
&lt;/h2&gt;

&lt;p&gt;The Big O notation describes complexity of an algorithm. Basically it tells you how fast the algorithm is. Simple enough. The &lt;strong&gt;O&lt;/strong&gt; in name stands for 'order of' or 'Ordnung'. It comes from the German number theoretician &lt;a href="https://en.wikipedia.org/wiki/Edmund_Landau" rel="noopener noreferrer"&gt;Edmund Landau&lt;/a&gt; and later it was adopted to Computer Science world.&lt;/p&gt;

&lt;p&gt;As a programmer you use or would use many algorithms written by other people. Consider that you would use another person's solution (&lt;em&gt;i.e. from &lt;a href="https://stackoverflow.com/" rel="noopener noreferrer"&gt;stackoverflow&lt;/a&gt;&lt;/em&gt;). Then you would love to know how fast the algorithm is.&lt;/p&gt;

&lt;p&gt;Take a look at the first trivial example. You have a sheet of paper and you have to divide it into 16 parts. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fvis47egcxpov2t9rgeyz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fvis47egcxpov2t9rgeyz.png" alt="sheet of paper"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How would you do that? The very first thought is that you can take a pen and write 16 squares one by one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F8cezvrafjlad43pg51zh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F8cezvrafjlad43pg51zh.png" alt="drawing squares"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you think about it as an algorithm you will realize that this is not an optimal solution. You would have to make 16 steps to finish the task. &lt;/p&gt;

&lt;p&gt;Someone else could say that you can fold the sheet 4 times and you also would have the sheet divided in 16 squares.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr25bxen7xvxu1cvcofsm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fr25bxen7xvxu1cvcofsm.png" alt="folding the paper"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you have 2 solutions, one better that another and you would like to describe them. With Big O notation it is really simple.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. What Big-O notation really describes?
&lt;/h2&gt;

&lt;p&gt;Imagine that you are a librarian and you have to find Mr. Smith's library card. What would you do if in a drawer there would be 1000 unordered cards and somewhere inside the Smith's card is hidden. First thought is that you would have to check every card one by one until you'll find the right card. Maybe the first one is the card that you are looking for, which is good, but maybe it is the last one... You would have to check 1000 then. It is the worst scenario and Big-O notation describes exactly this - &lt;strong&gt;the worst scenario&lt;/strong&gt; of an algorithm.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkiciu02vro0zsl4av2b2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkiciu02vro0zsl4av2b2.png" alt="The Big-O notation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If &lt;strong&gt;n&lt;/strong&gt; in this notation is a number of operations (&lt;em&gt;in the worst case&lt;/em&gt;) we can say that our algorithm has a complexity of &lt;strong&gt;O(1000)&lt;/strong&gt; which in general is just &lt;strong&gt;O(n)&lt;/strong&gt; because the number 1000 is a variable. &lt;/p&gt;

&lt;h4&gt;
  
  
  How would we describe an example with a sheet of paper?
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;O(n)&lt;/strong&gt; - scenario with a pen and drawing squares one by one.&lt;br&gt;
&lt;strong&gt;O(log2(n))&lt;/strong&gt; - scenario with folding the sheet of paper. &lt;/p&gt;

&lt;h2&gt;
  
  
  4. The most common complexities.
&lt;/h2&gt;

&lt;p&gt;Below you have the most common examples of the Big-O notation shown in a graph. I decided to use this graph because it perfectly shows why i.e &lt;strong&gt;O(n)&lt;/strong&gt; is better than &lt;strong&gt;O(n!)&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxur2r6sgc8svqhti0zgj.png" alt="The most common algorithms complexities"&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Source: &lt;a href="https://lukasmestan.com/simple-guide-to-big-o-notation/" rel="noopener noreferrer"&gt;lukasmestan.com&lt;/a&gt;&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The graph is simple to read. If there are more elements (&lt;strong&gt;n&lt;/strong&gt;) the algorithm is obligated to make more operations. Let's compare the best case &lt;strong&gt;O(log2(n))&lt;/strong&gt; and the mentioned &lt;strong&gt;O(n)&lt;/strong&gt;. &lt;a href="https://en.wikipedia.org/wiki/Binary_search_algorithm" rel="noopener noreferrer"&gt;Binary search&lt;/a&gt; is a perfect example of &lt;strong&gt;O(log2(n))&lt;/strong&gt; complexity. We can see in the plot above that it is almost constant even if we multiply the number of elements in algorithm. See the example below where we are looking for a number &lt;strong&gt;37&lt;/strong&gt; using binary search compared with sequential search.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.mathwarehouse.com%2Fprogramming%2Fimages%2Fbinary-vs-linear-search%2Fbinary-and-linear-search-animations.gif" alt="O(logn) vs (O)n"&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Source: &lt;a href="https://www.mathwarehouse.com/programming/images/binary-vs-linear-search/binary-and-linear-search-animations.gif" rel="noopener noreferrer"&gt;mathwarehouse&lt;/a&gt;&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The situation with binary search (&lt;strong&gt;O(log2(n))&lt;/strong&gt;) is almost perfect because no matter how many elements we would have, the number of made operations would not grow so much. Believe me or not but if we would have one milion elements in above example in binary search we would have to make at most 20 (&lt;em&gt;&lt;strong&gt;O(log2(1 000 000))&lt;/strong&gt; = ~20&lt;/em&gt;) operations to find a number we are looking for. AMAZING!&lt;/p&gt;

&lt;p&gt;With this knowledge you can chose the best algorithm.&lt;/p&gt;

&lt;h4&gt;
  
  
  You can say that there is no chance that you decide to use &lt;strong&gt;O(n!)&lt;/strong&gt; algorithm.
&lt;/h4&gt;

&lt;p&gt;Well, there is a chance. There is a problem called &lt;a href="https://en.wikipedia.org/wiki/Travelling_salesman_problem" rel="noopener noreferrer"&gt;Travelling Salesman Problem&lt;/a&gt;. It answered the question &lt;em&gt;,,Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city?"&lt;/em&gt;. This can be solved only by the algorithm with &lt;strong&gt;O(n!)&lt;/strong&gt; complexity.&lt;/p&gt;

&lt;p&gt;Despite of that you always would like to use algorithm of &lt;strong&gt;O(log2(n))&lt;/strong&gt; complexity, but sometimes it is not possible because some problems required more complicated solutions.&lt;/p&gt;

&lt;p&gt;This article was inspired by a book &lt;a href="https://www.amazon.ae/Grokking-Algorithms-Aditya-Y-Bhargava/dp/1617292230" rel="noopener noreferrer"&gt;,,Grokking Algorithms"&lt;/a&gt; written by &lt;em&gt;Aditya Y. Bhargava&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>What are Single Page Applications(SPA)?</title>
      <dc:creator>Paweł</dc:creator>
      <pubDate>Sun, 15 Sep 2019 09:18:42 +0000</pubDate>
      <link>https://dev.to/kendyl93/what-are-single-page-applications-spa-32bh</link>
      <guid>https://dev.to/kendyl93/what-are-single-page-applications-spa-32bh</guid>
      <description>&lt;p&gt;Have you ever heard about some of JavaScript frameworks, such as AngularJS, React or Vue.js? Maybe you would like to learn it but you don't know in general why there are so popular? All of them have adopted SPA principles. What is SPA? Let's start from the beginning...&lt;/p&gt;

&lt;h1&gt;
  
  
  The 'old' websites
&lt;/h1&gt;

&lt;p&gt;Let's assume that you have been developing the websites in the 'old' way. The end user opens it in the browser and navigate through it. &lt;/p&gt;

&lt;p&gt;Aside graphical interface the whole page was downloaded from the server. Every click resulted in sending a bunch of files by the server. Moreover if the user has bad internet connection this exchange might cause seconds to load the page! This is how it have been working behind the scenes.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffaav73ifc3ss6pjcl35l.png" alt="Traditional http request"&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Source: &lt;a href="https://msdn.microsoft.com/en-us/magazine/dn463786.aspx" rel="noopener noreferrer"&gt;microsoft&lt;/a&gt;&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Wouldn't it be nice to involve the server only once to load the page and navigate through it ? This is where SPA comes in.&lt;/p&gt;

&lt;h1&gt;
  
  
  What actually is SPA?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Single-page_application" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt; provides rather clear definition but let's take a closer look&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;,,A single-page application (SPA) is a web application or web site that interacts with the user by dynamically rewriting the current page rather than loading entire new pages from a server."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's dive deeper.&lt;/p&gt;

&lt;p&gt;SPA loads all the bundled content for the first time when you make a request by typing i.e. &lt;code&gt;www.myAwesomeSPAWebsite.com&lt;/code&gt; in the browser. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F33jxnuw76ahwv9dacedu.png" alt="SPA http request"&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Source: &lt;a href="https://msdn.microsoft.com/en-us/magazine/dn463786.aspx" rel="noopener noreferrer"&gt;microsoft&lt;/a&gt;&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can see that after the initial response from the server later on it would send the &lt;code&gt;JSON&lt;/code&gt; data. Most of the &lt;code&gt;requests&lt;/code&gt; in the app are moved to the client side. Server is mostly responsible for authentication and connecting with database.&lt;/p&gt;

&lt;p&gt;In popular SPA frameworks such as React, Vue, Angular, code of the website is split in &lt;strong&gt;components&lt;/strong&gt;. In general when you navigate through the page, other components are rendered replacing the content instead of download it from the server and reloading the whole page.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;img src="https://i.giphy.com/media/b2CD0Qrq2ulwY/giphy.gif" alt="SPA components"&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Source: &lt;a href="https://media.giphy.com/media/b2CD0Qrq2ulwY/giphy.gif" rel="noopener noreferrer"&gt;giphy&lt;/a&gt;&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h1&gt;
  
  
  SPA pros
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Using the website is faster&lt;/strong&gt; - Because everything is loaded by the first time the client can change the content on the page instantly. In other words our app works like the desktop application because it doesn't need to communicate with the server again to properly display the page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Decreases the server load&lt;/strong&gt; - SPA works different with the server at the communication level than the traditional page. To understand this better we can compare this process with building a house. Imagine that you(&lt;em&gt;client&lt;/em&gt;) would have to carry bricks(&lt;em&gt;data&lt;/em&gt;) one by one wasting your time(&lt;em&gt;like in traditional websites&lt;/em&gt;) or you can just use the wheelbarrow(&lt;em&gt;all of the data&lt;/em&gt;) and load all of them at once(&lt;em&gt;using SPA&lt;/em&gt;). Now you have to transport them only once at the beginning. From now on you do not care that a bricklayer sends you to bring some bricks again. You (&lt;em&gt;the server&lt;/em&gt;) can rest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User experience (UX)&lt;/strong&gt; - UX refers to any interaction a user has with a product or a service. The fact that you don't have to wait till the page reloads, gives you more pleasure of using it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Less amount of transmitted data&lt;/strong&gt; - I have mentioned that all the data is loaded at at once at the beginning. Moreover there are mechanisms that allows us to define which data should be loaded on the first request and which can be load later. It also increases the UX of the app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modern solutions&lt;/strong&gt; - Thanks to use modern solutions the code of an app can be more minified. You are also able to use &lt;a href="https://medium.com/@tomsoderlund/micro-frontends-a-microservice-approach-to-front-end-web-development-f325ebdadc16" rel="noopener noreferrer"&gt;&lt;em&gt;microservices&lt;/em&gt;&lt;/a&gt;. This gives you a possibility to divide your app into services that provide an API.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  SPA cons
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Developers pay more&lt;/strong&gt; - SPA can be simply written in &lt;em&gt;HTML&lt;/em&gt;, &lt;em&gt;JS&lt;/em&gt; and &lt;em&gt;CSS&lt;/em&gt; but sooner or later you will realize that you need to learn i.e. &lt;em&gt;Webpack&lt;/em&gt;, &lt;em&gt;Express&lt;/em&gt;, &lt;em&gt;React/Vue/Angular&lt;/em&gt; and other accompanying solutions. This can be frustrating at the beginning but it is worth to learn them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SEO&lt;/strong&gt; - Search engine &lt;a href="https://en.wikipedia.org/wiki/Web_crawler" rel="noopener noreferrer"&gt;crawlers&lt;/a&gt; like &lt;em&gt;Googlebot&lt;/em&gt; and &lt;em&gt;Bingbot&lt;/em&gt; are designed to index web pages through a process known as &lt;em&gt;spidering&lt;/em&gt; or &lt;em&gt;crawling&lt;/em&gt;, where they download the page’s HTML file(s). That's why it is easier to index and rank a static HTML-based web page. When it comes to indexing JavaScript-based SPAs, things get a little complicated. You have to make more effort to take care about the SEO.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Should you use SPA for your future projects?&lt;/strong&gt;&lt;br&gt;
Well, as always you should consider every pros and cons to choose the best option. Maybe it is not worth to develop SPA, maybe the overall cost would be too much. There can be more subjective pros and cons depending on the developer preferences. However it is worth to keep track this rapidly developing SPA world.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
