<?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: ufko</title>
    <description>The latest articles on DEV Community by ufko (@ufko).</description>
    <link>https://dev.to/ufko</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%2F237343%2F55cd2ada-4eb5-42ea-a63d-9c815a552f1d.png</url>
      <title>DEV Community: ufko</title>
      <link>https://dev.to/ufko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ufko"/>
    <language>en</language>
    <item>
      <title>🍻 Pub Sub 🍻</title>
      <dc:creator>ufko</dc:creator>
      <pubDate>Thu, 12 Dec 2019 08:28:49 +0000</pubDate>
      <link>https://dev.to/ufko/pub-sub-209k</link>
      <guid>https://dev.to/ufko/pub-sub-209k</guid>
      <description>&lt;p&gt;A terrible pun intended ☝️&lt;/p&gt;

&lt;p&gt;Have you ever joined a mailing list or followed an account on social media? Have you ever subscribed to a channel on YouTube?&lt;/p&gt;

&lt;p&gt;If your answer is "Yes!" to any of the questions above then congrats! 🎉 You pub-subbed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pub-subbed [ 📢 &lt;em&gt;puhb-suhbd&lt;/em&gt; ]&lt;br&gt;
⚪ &lt;em&gt;verb&lt;/em&gt;&lt;br&gt;
Becoming a part of publish-subscribe messaging system.&lt;br&gt;
Creating a publish-subscribe messaging system.&lt;br&gt;
"He &lt;em&gt;pub-subbed&lt;/em&gt; his way out of the problem."&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EF0XO1RS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mocfwrq0lwvsk0txk3hs.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EF0XO1RS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mocfwrq0lwvsk0txk3hs.jpg" alt="Mailboxes"&gt;&lt;/a&gt;&lt;/p&gt;
More accurate image to represent pub/sub



&lt;p&gt;&lt;strong&gt;What is pub/sub?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Pub/sub in its full form publish/subscribe is a messaging pattern. It is a way for different components of a system to notify each other and it's a great alternative to polling.&lt;/p&gt;

&lt;p&gt;In this pattern there are - you guessed it - publishers📤 and subscribers📥. In addition to them there are messages✉️ and channels/topics 📫. &lt;/p&gt;

&lt;p&gt;💯 Publishers can have any amount of subscribers&lt;br&gt;
 🕵️‍♂️ Publishers don't have to know about their subscribers&lt;br&gt;
 ⬅️ Publishers send messages to channels or topics to which subscribers can subscribe&lt;/p&gt;




&lt;p&gt;💯 Subscribers can subscribe to any amount of publishers&lt;br&gt;
 🕵️‍♂️ Subscribers don’t need to know who the message comes from&lt;br&gt;
 ➡️ Subscribers receive messages from channels or topics to which publishers send messages&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are the advantages of pub/sub?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;✅ Decoupling/Loose coupling&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Since neither the publishers nor the subscribers have to know about each other pub-subbing helps with the decoupling of system components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ Scalability&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When number of subscribers or channels/topics increases adding new servers to the system helps with dealing with the increased load.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ Asynchronicity&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Since publisher sends its messages to a channel or topic it doesn't have to wait for a subscriber to receive the message. And since the subscriber receives messages from a channel/topic it doesn't
have to wait for a message. In fact it doesn't have to be up when the message was sent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do all these remind you something? &lt;em&gt;Hint: The answer is a design pattern.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;/p&gt;

&lt;p&gt;The answer is: Observer!&lt;/p&gt;

&lt;p&gt;Pubsub is similar to Observer design pattern of Gof 🙆‍♂️.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🔒 Which problems does Observer solve? 🔒&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;From Gof&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How can a one-to-many dependency between objects be defined without making the objects tightly coupled?&lt;/li&gt;
&lt;li&gt;How can an object notify an open-ended number of other objects?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;🔑 How does Observer solve these problems? 🔑&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;From Gof&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define Subject and Observer objects 

&lt;ul&gt;
&lt;li&gt;Subject -&amp;gt; Publisher and channel/topic&lt;/li&gt;
&lt;li&gt;Observer -&amp;gt; Subscriber&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;When a subject changes state, all registered observers are notified and updated automatically

&lt;ul&gt;
&lt;li&gt;Subject changes state -&amp;gt; Publisher sends a message to channel/topic&lt;/li&gt;
&lt;li&gt;Observers are notified and updated -&amp;gt; Subscribers are notified and updated&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Similarities&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Just like in pubsub, in observer the subjects don't have to know about their observers and vice versa.&lt;/li&gt;
&lt;li&gt;Just like in pubsub, in observer the subjects can have any amount of observers and vice versa.&lt;/li&gt;
&lt;li&gt;Just like in pubsub, subjects and observers are loosely coupled.&lt;/li&gt;
&lt;li&gt;Just like in pubsub, observers can be updated asynchronously.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you're not sick of me associating things, both observer pattern and pub/sub messaging reminds me of the mailing systems in human world 🤷‍♂️. It's really cool to see concepts from computing world resemble human world 💻 🌎.&lt;/p&gt;




&lt;h6&gt;
  
  
  Cover Image: Photo by Mary Rebecca Elliott on Unsplash
&lt;/h6&gt;

&lt;h6&gt;
  
  
  Body Image: Photo by Mathyas Kurmann on Unsplash
&lt;/h6&gt;

</description>
    </item>
    <item>
      <title>Javascript uses call by sharing for objects</title>
      <dc:creator>ufko</dc:creator>
      <pubDate>Fri, 27 Sep 2019 17:53:50 +0000</pubDate>
      <link>https://dev.to/ufko/javascript-uses-call-by-sharing-for-objects-30og</link>
      <guid>https://dev.to/ufko/javascript-uses-call-by-sharing-for-objects-30og</guid>
      <description>&lt;p&gt;I recently watched a course about javascript on Udemy. It was a great course from a great instructor who provided great content - except for one thing. He said that &lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Javascript is pass by reference for objects and pass by value for primitives."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This statement is only 50% correct 🌓 The incorrect half - as it is hinted in the title of the post - is the first half. Javascript doesn't use pass by reference for objects. It is commonly stated that javascript uses pass by value for objects too and that the value for objects is actually the reference of that object. That's what I thought about javascript's evaluation strategy, too. But that statement is not 100% correct either 😲😓💥 TIL it uses &lt;strong&gt;call by sharing&lt;/strong&gt; for objects 😲 💥&lt;/p&gt;

&lt;p&gt;So let's dive into these aforementioned evaluation strategies and try to understand the differences between them and hopefully clear up the confusion:&lt;/p&gt;

&lt;h2&gt;
  
  
  Pass by reference vs. Pass by value vs. Call by sharing
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Pass by reference
&lt;/h4&gt;

&lt;p&gt;I want to use an analogy here. Let's consider soccer. In soccer there is one ball and players pass the ball to each other (to simply put it). If the ball so to say gets muddy, dirty and a player passes the ball to another player the second player will receive the muddy, dirty ball. And &lt;br&gt;
let's assume the players decided to clean the ball because it got so dirty that it made it impossible play the game. When game continues with the clean ball none of the players will receive the old dirty, muddy ball they will all be playing with new clean and shiny ball.&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;pass by reference&lt;/strong&gt;. The ball here is the reference. In pass by reference function receives a reference to the variable. If any change occurs in the reference every piece of code which uses that reference will be affected and will use the changed version of the reference.&lt;/p&gt;
&lt;h4&gt;
  
  
  Pass by value
&lt;/h4&gt;

&lt;p&gt;Let's use another analogy here. Let's consider books. A person writes a book and publishes it. If you want to read that book you go to the bookstore and get yourself a copy of that book. You don't go to the author's house and grab the original writings. And if you decide to underline a sentence in your book the other books don't get the same sentence underlined, you only change your copy. &lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;pass by value&lt;/strong&gt;. The book here is the value. In pass by value function receives a copy of the value. If the function changes the value, the change will be limited in the function's scope.&lt;/p&gt;
&lt;h4&gt;
  
  
  Call by sharing
&lt;/h4&gt;

&lt;p&gt;Unfortunately I don't have an analogy for this evaluation strategy 😥 So I'm just going to write it as it is.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;call by sharing&lt;/strong&gt; the function receives a copy of the reference to the object. If the object gets mutated, every piece of code which uses that object will use the changed version of the object.&lt;/p&gt;



&lt;p&gt;By now the evaluation strategies playing part in the confusion and the differences they have should be clear which means we can skip to the reason behind the confusion part 🎉🎊 Let's consider the following code snippet:  &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;When &lt;code&gt;john&lt;/code&gt; is passed to &lt;code&gt;confusionCreator()&lt;/code&gt;, actually the reference to &lt;code&gt;john&lt;/code&gt; is &lt;strong&gt;copied&lt;/strong&gt; and passed to &lt;code&gt;confusionCreator()&lt;/code&gt;. Since there is a copying process it resembles &lt;strong&gt;pass by value&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;When &lt;code&gt;name&lt;/code&gt; attribute is updated inside &lt;code&gt;confusionCreator()&lt;/code&gt; but &lt;strong&gt;affected&lt;/strong&gt; the code pieces using &lt;code&gt;john&lt;/code&gt; outside the &lt;code&gt;confusionCreator()&lt;/code&gt; it resembles &lt;strong&gt;pass by reference&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These resemblances are the reason behind the confusion around javascript's evaluation strategies. &lt;/p&gt;

&lt;p&gt;I hope this post helped in some way to clear up the confusion around javascript's evaluation strategies.&lt;/p&gt;




&lt;h6&gt;
  
  
  Cover Photo by Hans-Peter Gauster on Unsplash
&lt;/h6&gt;

</description>
      <category>javascript</category>
      <category>todayilearned</category>
      <category>top7</category>
    </item>
  </channel>
</rss>
