<?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: Rahil Rehan</title>
    <description>The latest articles on DEV Community by Rahil Rehan (@rahil_rehan).</description>
    <link>https://dev.to/rahil_rehan</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%2F368716%2Ff352b173-f404-4a34-915c-aa764fdb072f.jpeg</url>
      <title>DEV Community: Rahil Rehan</title>
      <link>https://dev.to/rahil_rehan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rahil_rehan"/>
    <language>en</language>
    <item>
      <title>Part-0: Learn devops from scratch!</title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Wed, 09 Feb 2022 12:51:01 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/learn-devops-now-part-0-11lj</link>
      <guid>https://dev.to/rahil_rehan/learn-devops-now-part-0-11lj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this series of posts, I will share my journey of learning DevOps. Moreover, together, we will build some dummy microservices and also deploy them. On the way, we will explore important cloud-native tools.&lt;/p&gt;

&lt;h2&gt;
  
  
  How is this guide/tutorial different?
&lt;/h2&gt;

&lt;p&gt;Right now, I'm struggling to learn DevOps and to get into a cloud-native ecosystem. There are a ton of tools, and also a ton of great resources put up by amazing people. The only thing that I found missing was, no tutorial explains how everything works together. That's the problem I want to solve with this tutorial. Below is a rough draft of topics I might cover, more items and steps might get added in the future.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things we will cover:
&lt;/h2&gt;

&lt;p&gt;Note: Don't worry if you don't understand anything below. We will learn each one of them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build simple dummy microservices

&lt;ul&gt;
&lt;li&gt;Write some API's&lt;/li&gt;
&lt;li&gt;Write some tests&lt;/li&gt;
&lt;li&gt;Implement simple logging&lt;/li&gt;
&lt;li&gt;Best practices&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Containerize the application built above

&lt;ul&gt;
&lt;li&gt;Learn and use docker&lt;/li&gt;
&lt;li&gt;Use docker-compose&lt;/li&gt;
&lt;li&gt;Best practices&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Build continuous integration pipeline

&lt;ul&gt;
&lt;li&gt;Learn and use Jenkins or GitHub actions.&lt;/li&gt;
&lt;li&gt;Setup continuous integration for our apps&lt;/li&gt;
&lt;li&gt;Best practices&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Provisioning

&lt;ul&gt;
&lt;li&gt;Learn Terraform&lt;/li&gt;
&lt;li&gt;Provision resources on cloud providers&lt;/li&gt;
&lt;li&gt;Best practices&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Orchestration

&lt;ul&gt;
&lt;li&gt;Learn Kubernetes&lt;/li&gt;
&lt;li&gt;Setup Kubernetes on the cloud provider&lt;/li&gt;
&lt;li&gt;Deploy our apps to Kubernetes&lt;/li&gt;
&lt;li&gt;Best practices&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Packaging 

&lt;ul&gt;
&lt;li&gt;Learn helm&lt;/li&gt;
&lt;li&gt;Use helm&lt;/li&gt;
&lt;li&gt;Best practices&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Environments

&lt;ul&gt;
&lt;li&gt;Setup different environments like Dev, QA, Production&lt;/li&gt;
&lt;li&gt;Best practices&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;More things

&lt;ul&gt;
&lt;li&gt;I may not be able to cover the below items, but I will try!&lt;/li&gt;
&lt;li&gt;Ansible&lt;/li&gt;
&lt;li&gt;Secret management&lt;/li&gt;
&lt;li&gt;Centralized configuration&lt;/li&gt;
&lt;li&gt;Centralized Log management&lt;/li&gt;
&lt;li&gt;Metrics&lt;/li&gt;
&lt;li&gt;Tracing&lt;/li&gt;
&lt;li&gt;Security aspects&lt;/li&gt;
&lt;li&gt;... more? maybe!&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;Oops, looks like a big commitment, lol!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I won't go into much details about each and every tool as I feel there are ton of good resources to learn tools. I will point to good resources in each section.&lt;/p&gt;

&lt;h2&gt;
  
  
  The challenge
&lt;/h2&gt;

&lt;p&gt;It is going to take time to publish the whole playlist, and some blogs will contain a lot of information which might consume more time to write. &lt;/p&gt;

&lt;p&gt;Moreover, people tend to lose interest in incomplete blog playlists causing less retention and loss of interest in more interesting and important topics in the later parts. Keep this in mind, and try to read all the blogs to get the complete picture!&lt;/p&gt;

&lt;p&gt;If you think I have missed any topics, made some mistakes then please comment and share your views!&lt;/p&gt;

&lt;p&gt;If you want to motivate me more to sit and complete this series and help others find this resource, please leave a reaction. Thanks!&lt;/p&gt;

&lt;p&gt;twitter: &lt;a href="https://twitter.com/rahil_rehan"&gt;https://twitter.com/rahil_rehan&lt;/a&gt;&lt;br&gt;
personal blog: &lt;a href="https://rahilrehan.github.io/"&gt;https://rahilrehan.github.io/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Computer Networks simplified!</title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Sat, 22 Jan 2022 06:38:54 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/computer-networks-simplified-4koa</link>
      <guid>https://dev.to/rahil_rehan/computer-networks-simplified-4koa</guid>
      <description>&lt;h2&gt;
  
  
  Have you ever wondered how data is transferred from one device to another?
&lt;/h2&gt;

&lt;p&gt;Well, to answer this question we need to understand computer networks. In this blog, we will briefly discuss the entire process in one go.&lt;/p&gt;

&lt;p&gt;Before we get into any lower-level details, let's first get an overview of the process, and we'll dwell deep later.&lt;/p&gt;

&lt;h2&gt;
  
  
  The OSI reference model
&lt;/h2&gt;

&lt;p&gt;This is a proposed conceptual model that characterizes communication functions. &lt;/p&gt;

&lt;h3&gt;
  
  
  The 7 proposed layers:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Application Layer&lt;/li&gt;
&lt;li&gt;Presentation Layer&lt;/li&gt;
&lt;li&gt;Session Layer&lt;/li&gt;
&lt;li&gt;Transport Layer&lt;/li&gt;
&lt;li&gt;Network Layer&lt;/li&gt;
&lt;li&gt;Data-link Layer&lt;/li&gt;
&lt;li&gt;Physical Layer&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Again, this is a proposed layer. Real communication models(like TCP/IP model) may not have all the layers, or more interestingly some layers combined to form one.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Application Layer
&lt;/h3&gt;

&lt;p&gt;Let's first understand what protocol means before talking about the application layer.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Protocol: *&lt;/em&gt;&lt;br&gt;
The protocol is a set of rules agreed upon by all parties for some process. Once a protocol is proposed, parties use the same defined steps to perform a task.&lt;/p&gt;

&lt;p&gt;In the application layer, there are a bunch of protocols that are used for various tasks.&lt;/p&gt;

&lt;p&gt;For example, the Gmail application may be using SMTP(Simple Mail Transfer Protocol) to send and receive emails, and all this happens at the application level(here the application is Gmail or it can be your browser). So, this protocol must be supported by the application itself.&lt;br&gt;
The same concept applies to HTTP and HTTPS(which are already baked into browsers).&lt;/p&gt;

&lt;p&gt;Some more examples of protocols in the layer: SMTP, HTTP, HTTPS, Telnet, RDP, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Presentation Layer
&lt;/h3&gt;

&lt;p&gt;Some very important services like translation, data compression, encryption, etc are provided by this layer. &lt;/p&gt;

&lt;p&gt;Data after going through specified steps in protocols from the application layer undergo the below steps in the presentation layer now.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Translation&lt;/strong&gt;&lt;br&gt;
Data that is in text format(Unicode or utf-8) is converted to binary form.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compression&lt;/strong&gt; &lt;br&gt;
Binary data from the above steps are compressed algorithms like Huffman encoding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Encryption&lt;/strong&gt; Encryption of secret data(like passwords, SSL certificates) is part of this process. Symmetric and asymmetric encryption protocols act here.&lt;/p&gt;

&lt;p&gt;... and many more, there are tons of other things which happen here but these were the important ones.&lt;/p&gt;

&lt;p&gt;A lot of fancy buzz happens at the presentation layer. Perfect naming for the layer, lol!&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Session Layer
&lt;/h3&gt;

&lt;p&gt;Establish, Manage, and terminate session connections between end-user applications.&lt;/p&gt;

&lt;p&gt;More importantly, things like token management, synchronization, authentication, authorization are the responsibilities of this layer.&lt;/p&gt;

&lt;p&gt;We can say, this layer is important for security and synchronization.&lt;/p&gt;

&lt;p&gt;Note: In a TCP/IP model, the browser bake in all the above three layers functionalities into just one layer called the application layer.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Transport Layer
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Segmentation&lt;/strong&gt;: &lt;br&gt;
Data is broken down into segments(small pieces) and sent over the network.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flow Control&lt;/strong&gt;:&lt;br&gt;
Devices may have different networking capabilities and maybe one device can operate faster(like send data at 100mbps) than the other(10mbps). Well, the transport layer takes care of this problem. We will explore how later.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Error Control&lt;/strong&gt;: &lt;br&gt;
While transferring segments, some segments may get lost! Again, the transport layer got our back. It uses checksums to find such errors in transmission.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Port numbers&lt;/strong&gt;: &lt;br&gt;
The segments may get confused when they reach the destination device as they don't know their destination application. To avoid this confusion, we assign port numbers to segments before sending them from the source. So when a segment reaches a destination device it knows which application it should go to.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, a segment looks like this &lt;br&gt;
Segment = (source port + destination port + sequence number + acknowledgment + length) &lt;br&gt;
Wireshark example: Src Port: 6648, Dst Port: 443, Seq: 24, Ack: 251, Len: 23&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Network Layer
&lt;/h3&gt;

&lt;p&gt;After segments are made we attach some more information to the segment, this includes the source IP and destination IP.&lt;/p&gt;

&lt;p&gt;Just like the port number which helps segments to reach a particular application on the destination device, IP addresses help reach a particular device on the network.&lt;/p&gt;

&lt;p&gt;(segment + source and destination IP) together is called a packet. An IP address is just 32 bits which can uniquely determine about 4294967296 devices, but there are more devices than that number on the internet and that's where IPV6 pitches in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IP(Internet Protocol)&lt;/strong&gt; is again just unique over a local network but not on the whole internet. Just remember this, we will know more about it in further blogs.&lt;/p&gt;

&lt;p&gt;Routing, DNS, Masking are also functionalities provided by this layer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Routing&lt;/strong&gt;: helps in the path determination of packet. i.e. how the packet should reach from source to destination and what routes/hops it should take over the network.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DNS&lt;/strong&gt;: it is a map of key-value pairs. example: &lt;a href="http://www.google.com(key):192.168.32.3(value)"&gt;www.google.com(key):192.168.32.3(value)&lt;/a&gt;. Instead of us remembering all the IP addresses of different websites, we have DNS to rescue. For example, &lt;a href="http://www.google.com"&gt;www.google.com&lt;/a&gt; is resolved to an IP from the DNS server when we make a request.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Data Link Layer
&lt;/h3&gt;

&lt;p&gt;As we already discussed IP addresses are just unique over the network but not over the internet, we need one more level of uniqueness(remember there are millions of devices!). We use the MAC address which is 48 bits! and every device which has networking capabilities is manufactured with its unique mac address on the entire internet. phew!!&lt;/p&gt;

&lt;p&gt;Now finally, we have something we call a frame, a combination of packet + source and destination mac addresses + some extra tail data(like some more checksum to avoid data loss at this level).&lt;/p&gt;

&lt;p&gt;some more functionalities provided by this layer are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Access to media&lt;/strong&gt;: Determination of media for transfers (wired, wireless, optical, etc)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Media-Access Control&lt;/strong&gt;: avoiding collision over the media.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Error detection&lt;/strong&gt;: tail data is used to perform checksum here.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  7. Physical Layer:
&lt;/h3&gt;

&lt;p&gt;Bit-level data transfer happens at this layer, i.e bits of frames are converted into some form of signals. Ex: light flashes or radio signals and then transmitted. &lt;/p&gt;

&lt;p&gt;We won't discuss much of the physical layer as I don't think it is that important for Software Engineering. If you are interested in exploring, be my guest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transmission Media&lt;/strong&gt;: What media to use to transport data?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Guide&lt;/em&gt;: There is a physical medium like wires(co-axial, twisted pair cables) on which transmission takes place.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Unguided&lt;/em&gt;: Medium of transfer is air, examples are Wifi, infrared and radio signals.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Transmission Modes&lt;/strong&gt;:&lt;br&gt;
When two devices are connected over a medium we can have three modes of transmission&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Simplex&lt;/em&gt;: Data can flow only in one direction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Half-duple&lt;/em&gt;: Data can flow in both directions but not at the same time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Full-duplex&lt;/em&gt;: Data can flow in both directions and simultaneously&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would highly recommend you to read about Manchester encoding and differential Manchester encoding to get an understanding of how signals can carry bit information.&lt;/p&gt;

&lt;p&gt;Data is processed sequentially from the application layer to the physical layer and then sent over to another device. Now data is decodedbackwardsd. i.e data is decoded from the physical layer to the application layer. &lt;/p&gt;

&lt;p&gt;It is just like amazon delivery, packing the product and delivering it to the customer and when the customer receives the product they unbox just relate and you will be surprised. We will dive deep soon, hopefully! See you again!&lt;/p&gt;

&lt;p&gt;Visit my blog at: &lt;a href="https://rahilrehan.github.io/"&gt;https://rahilrehan.github.io/&lt;/a&gt; 🙈&lt;br&gt;
And follow me on Twitter: &lt;a href="https://twitter.com/rahil_rehan"&gt;https://twitter.com/rahil_rehan&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Software Development Lifecycle</title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Tue, 19 Oct 2021 08:54:13 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/software-development-lifecycle-514o</link>
      <guid>https://dev.to/rahil_rehan/software-development-lifecycle-514o</guid>
      <description>&lt;h2&gt;
  
  
  What is Software Development/Engineering?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Software engineering is defined as the application of a systematic, disciplined, quantifiable approach to the development, operation, and maintenance of software.&lt;/em&gt; - IEEE's Standard Glossary&lt;/p&gt;

&lt;p&gt;Software devlopment is not just writing code, its about writing clean, understandable and maintainable code. And there are many other things involved in software development process. Things like requiremnt gathering, planning, desinging, testing, releasing and later maintaining. These aspects are also very important!&lt;/p&gt;

&lt;p&gt;Like Martin Fowler said,  “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of software engineers:
&lt;/h3&gt;

&lt;p&gt;SDE(Software development engineer) - A common term used to represent an umbrella of professions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Frontend engineers: Folks who deal with visual components of the product.&lt;/li&gt;
&lt;li&gt;Backend engineers: Folks who deal with internal system components providing an abstraction in form of APIs which frontend engineers can use.&lt;/li&gt;
&lt;li&gt;Fullstack engineers: Combination of both of the above roles.&lt;/li&gt;
&lt;li&gt;System Architect: The system designer, makes sure the systems design is good .&lt;/li&gt;
&lt;li&gt;SDET(tester): One who test the whole system for bugs and acceptance critereas.

&lt;ul&gt;
&lt;li&gt;In some companies testing is part of development process, so this role is played by SDE itself. Ex: in TDD&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Operations engineer: Deals with releasing the product to market. Deployment and stuff.&lt;/li&gt;
&lt;li&gt;SRE - Site reliability engineers: Folks who work both on development teams and operations teams(release teams)&lt;/li&gt;
&lt;li&gt;Devops engineer: Acts as a middleman between devolopment teams and operations team. It's more of a culture. So, some companies may not have this role at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Testing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Acceptance tests: end-to-end tests to ensure the whole system adheres to business requirements.&lt;/li&gt;
&lt;li&gt;Performance, Load, Regression testing: Even though all the test cases pass, system has to be performant, should be able to handle load etc. These tests evaluate above metrics.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Software Development Models:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Waterfall model:
&lt;/h3&gt;

&lt;p&gt;Steps in waterfall model:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Requirement collection (gather customer requirement)&lt;/li&gt;
&lt;li&gt;Desgin (design system accordingly)&lt;/li&gt;
&lt;li&gt;Implementation (build system)&lt;/li&gt;
&lt;li&gt;Verification (verify if the product meets the initial requriements)&lt;/li&gt;
&lt;li&gt;Maintenance (Maintain the project)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this model we assume that all requirements are collected before implementation (which is almost never the case!)&lt;/p&gt;

&lt;h3&gt;
  
  
  Iterative Model:
&lt;/h3&gt;

&lt;p&gt;Steps in iterative model:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Requirement collection&lt;/li&gt;
&lt;li&gt;Design&lt;/li&gt;
&lt;li&gt;Implement &lt;/li&gt;
&lt;li&gt;Verify&lt;/li&gt;
&lt;li&gt;Jump to 1st step(and repeat)&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Repeat this until the customer is satisfied ^^&lt;/li&gt;
&lt;li&gt;Problem from waterfall model is solved!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Agile!
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Agile itself is not a model, it is an umbrella term.&lt;/li&gt;
&lt;li&gt;Agile way says,

&lt;ul&gt;
&lt;li&gt;we have to build software in iterative, incrementing, short cycles.&lt;/li&gt;
&lt;li&gt;Intra-team(cross-team) communication is key to reduce conflicts in future.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h3&gt;
  
  
  Some models of agile:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Lean development:[copied from wiki]
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Eliminate waste : do not add anything which is not required.&lt;/li&gt;
&lt;li&gt;Amplify learning: gather requirments constantly, share knowledge among teams.&lt;/li&gt;
&lt;li&gt;Decide as late as possible: conter-intuitive? nah, just don't commit early.&lt;/li&gt;
&lt;li&gt;Deliver ASAP: the faster the deliver, the faster the feedback and faster the cycle repeats.&lt;/li&gt;
&lt;li&gt;Empower the team: Important.&lt;/li&gt;
&lt;li&gt;Optimize the whole: Always think about how the system works on the whole.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  SCRUM!!
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The whole project is broken down into chunks. &lt;/li&gt;
&lt;li&gt;Each chunk is divided such that it can be completed in 2-4 weeks.&lt;/li&gt;
&lt;li&gt;And this timeframe to complete a chuck is called &lt;code&gt;sprint&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Each chunk of work is again divided into tasks which people can pickup and complete.&lt;/li&gt;
&lt;li&gt;Number of tasks completed in a sprint gives teams &lt;code&gt;velocity&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Balance the velocity, it shouldn't be too high(easy tasks) or too low(hard tasks).&lt;/li&gt;
&lt;li&gt;The tasks which are not completed(spillovers) are again put into next sprint.&lt;/li&gt;
&lt;li&gt;Scrum events:

&lt;ul&gt;
&lt;li&gt;Planning - Initial planning of whole project&lt;/li&gt;
&lt;li&gt;Estimation - Splitting into chunks&lt;/li&gt;
&lt;li&gt;Daily standups - Daily updates(what was done yesterday, what will be done today, any blockers?) - each team member talks about these three points in short.&lt;/li&gt;
&lt;li&gt;Retrospection - After each sprint, what went well and what can be improved must be discussed among team. And actions items have to be planned accordingly.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h4&gt;
  
  
  Kanban
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;A board style todo-list.&lt;/li&gt;
&lt;li&gt;Contains lanes

&lt;ul&gt;
&lt;li&gt;TODO/Backlog
&lt;/li&gt;
&lt;li&gt;Doing&lt;/li&gt;
&lt;li&gt;Review&lt;/li&gt;
&lt;li&gt;Done&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;More lanes can be added according to teams need and style&lt;/li&gt;
&lt;li&gt;Ex: Trello&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Devops!
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Popular!&lt;/li&gt;
&lt;li&gt;People are switching to microservice architecture.&lt;/li&gt;
&lt;li&gt;As number of services increase in microservice architecture, each team which is responsible for a service owns the service. Hence, team should also be involved in operations related tasks!&lt;/li&gt;
&lt;li&gt;Devops itself has become a new role now-a-days. It was just a culture before...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Visit my personal blog: &lt;a href="https://rahilrehan.github.io/"&gt;https://rahilrehan.github.io/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sde</category>
    </item>
    <item>
      <title>Dependency Injection - Spring</title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Sat, 09 Oct 2021 02:59:09 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/dependency-injection-spring-271p</link>
      <guid>https://dev.to/rahil_rehan/dependency-injection-spring-271p</guid>
      <description>&lt;h3&gt;
  
  
  Basics
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Spring has lot of modules&lt;/li&gt;
&lt;li&gt;start.spring.io lists many spring modules which you can configure&lt;/li&gt;
&lt;li&gt;spring boot is useful to build RESTfull web services. It makes developing spring applications easy by auto configuring default components. It is opinionated framework&lt;/li&gt;
&lt;li&gt;fat jar -&amp;gt; also includes tomcat. better solution if you want to deploy on cloud platforms&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Dependency Injection
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Assume mail sender example, where we can have mailSender of type SMTP, POPM or MockMail. &lt;/li&gt;
&lt;li&gt;We can have an interface MailSender which defines the common functionality and we implement the interface in SMTP, POPM or Mock class.&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;Problem: object creation will be like: *&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;private MailSender  = new SMTPMailSender()&lt;/li&gt;
&lt;li&gt;private MailSender  = new POPMMailSender()&lt;/li&gt;
&lt;li&gt;private MailSender  = new MockMailSender()&lt;/li&gt;
&lt;li&gt;we need to hardcode the type while creating object&lt;/li&gt;
&lt;li&gt;can we automate this?&lt;/li&gt;
&lt;li&gt;Add @Component annotation to any class which implements MailSender&lt;/li&gt;
&lt;li&gt;Add @Autowired annotation when creating object&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;how does it work:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;When application starts all the beans gets setup

&lt;ul&gt;
&lt;li&gt;Beans: annotations at top level, whose object will be created and stored in &lt;em&gt;Application Context&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Beans examples: @Component, @RestController&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;When application is running and when we create objects variables for which we have annotations like @Autowired. Objects which were already created from application context are assigned to current variables. &lt;/li&gt;
&lt;li&gt;i.e those objects from application context are injected into these variables and hence dependency injection&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;@Autowired also works on methods(setters, constructors, getters, any methods..)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple beans problem:&lt;/strong&gt; when we create two classes from same interface and add @component annotation to both classes, and create object using interface using @autowired annotation, spring will be confused to choose between beans

&lt;ul&gt;
&lt;li&gt;one solution is to use camel-case variable names while creating objects which resemble the name of the class you want (ex: smtpMailSender)&lt;/li&gt;
&lt;li&gt;you can also add name param to @component -&amp;gt; @component("mockMail"), and create object using the name mockMail&lt;/li&gt;
&lt;li&gt;use extra &lt;a class="mentioned-user" href="https://dev.to/primary"&gt;@primary&lt;/a&gt; annotation, the bean which has this will get preference&lt;/li&gt;
&lt;li&gt;you can also use @qualifier annotation to class, and also use @qualifier in setter or constructor, ...etc&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Prefer constructor injection&lt;/li&gt;
&lt;li&gt;@resource and @inject are similar to @autowired, but @autowired is very powerful, so use @autowired&lt;/li&gt;
&lt;li&gt;Specializations of @component annotations

&lt;ul&gt;
&lt;li&gt;@Controller&lt;/li&gt;
&lt;li&gt;@Service&lt;/li&gt;
&lt;li&gt;@Repository&lt;/li&gt;
&lt;li&gt;@Configuration&lt;/li&gt;
&lt;li&gt;@SpringBootApplication(scanBasePackageClasses = {default.class, abc.class}) - scans through the package and puts any dependency injection components into application context&lt;/li&gt;
&lt;li&gt;@RestController&lt;/li&gt;
&lt;li&gt;..... many others&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;what if you import a dependency and you have to create object from there, you cannot add @component annotation dependency package

&lt;ul&gt;
&lt;li&gt;Create a new config class, which has methods to return objects from the external dependency&lt;/li&gt;
&lt;li&gt;the new config class must have @Configuration annotation&lt;/li&gt;
&lt;li&gt;and methods must have @Bean configuration&lt;/li&gt;
&lt;li&gt;names of the methods can be used for object variable names&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;use @Value annotation to access variables from .properties or .yaml files&lt;/li&gt;
&lt;li&gt;an external config file takes more preference over internal config(application.properties)&lt;/li&gt;
&lt;li&gt;**Profiles: **A spring application can be configure to behave differently in different environments using profiles

&lt;ul&gt;
&lt;li&gt;ex environments: dev, test, prod&lt;/li&gt;
&lt;li&gt;provide in application.properties:

&lt;ul&gt;
&lt;li&gt;spring.profiles.active: book, dev&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;you can also use particular beans in particular environments!

&lt;ul&gt;
&lt;li&gt;just  use &lt;a class="mentioned-user" href="https://dev.to/profile"&gt;@profile&lt;/a&gt;("envName"), can accepts bool, !envName&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;@Conditional annotation, include or exclude beans based on arbitrary conditions

&lt;ul&gt;
&lt;li&gt;@ConditionalOnClass, OnProperty, OnMissingBean ..etc&lt;/li&gt;
&lt;li&gt;configure bean based on presence of application property =&amp;gt; @ConditionalOnProperty&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;In @Configuration classes, when we try to create same bean again from some method, spring still returns the cached bean only.

&lt;ul&gt;
&lt;li&gt;Caching does not happen when we use @Component annotation &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Always use @Configuration Classes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;visit my personal blog: &lt;a href="https://rahilrehan.github.io/"&gt;https://rahilrehan.github.io/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
    </item>
    <item>
      <title>Test Driven Development - Notes</title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Sat, 09 Oct 2021 02:46:10 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/test-driven-development-notes-1cd5</link>
      <guid>https://dev.to/rahil_rehan/test-driven-development-notes-1cd5</guid>
      <description>&lt;ul&gt;
&lt;li&gt;The art of a good night's sleep is knowing you will not get woken by a support call and the piece of mind from being able to confidently change your software in an always moving market. - Nic Jackson&lt;/li&gt;
&lt;li&gt;change in code can have undesirable effect on the other parts, tests helps in making the process of changing code in future easy&lt;/li&gt;
&lt;li&gt;testing pyramid: unit testing(bottom), service/integration(middle), ui/end-end(top)

&lt;ul&gt;
&lt;li&gt;test cases should decrease as you go from bottom to top&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  Approach&lt;/h3&gt;
&lt;/li&gt;



&lt;li&gt;Just write test first (use expected function)
&lt;/li&gt;
&lt;li&gt;Run and fail the test
&lt;/li&gt;
&lt;li&gt;Write minimal code for the test to run and fail
&lt;/li&gt;
&lt;li&gt;Write minimal code to make test pass
&lt;/li&gt;
&lt;li&gt;Test same function with different values
&lt;/li&gt;
&lt;li&gt;Make sure all tests are passing
&lt;/li&gt;
&lt;li&gt;Tests for base cases
&lt;/li&gt;
&lt;li&gt;Refactor both code and tests&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Features and scenarios
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Given a feature, break it down to scenarios. Each feature is a user story

&lt;ul&gt;
&lt;li&gt;Example Feature: As a user when I call the search endpoint, I would like to receive a list of kittens&lt;/li&gt;
&lt;li&gt;Break it down to scenarios

&lt;ul&gt;
&lt;li&gt;1st Scenario: Invalid query
    Given I have no search criteria
    When I call the search endpoint
    Then I should receive a bad request message&lt;/li&gt;
&lt;li&gt;and so... on number of different scenarios&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h3&gt;
  
  
  Uncle bobs, rules of unit tests
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First law: You may not write production code until you have written a failing unit test &lt;/li&gt;
&lt;li&gt;Second law: You may not write more of a unit test than is sufficient to fail, and not compiling is failing&lt;/li&gt;
&lt;li&gt;Third law: You may not write more production code than is sufficient to pass the currently failing test&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Naming
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;my convention: TestNameReturnSomethingWhenSomethingHappens

&lt;ul&gt;
&lt;li&gt;example:  TestDogHandlerReturnsBadRequestWhenNoSearchCriteriaIsSent&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h3&gt;
  
  
  AAA format for testing - Arrange, Act, Assert
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;I always try to follow this pattern for clean code
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CalculatorTest&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sumOfTwoNumbers&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="c1"&gt;//Arrange&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="nc"&gt;Calc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Calculator&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;//Act&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Calc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Sum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//Assert&lt;/span&gt;
        &lt;span class="nc"&gt;Assert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Equal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  concepts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/1077412/what-is-an-idempotent-operation"&gt;Idempotency&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://softwareengineering.stackexchange.com/questions/40297/what-is-a-side-effect"&gt;Side effects&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;stub&lt;/strong&gt; is a fake class that comes with preprogrammed return values. It’s injected into the class under test to give you absolute control over what’s being tested as input. A typical stub is a database connection that allows you to mimic any scenario without having a real database.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;mock&lt;/strong&gt; is a fake class that can be examined after the test is finished for its interactions with the class under test. For example, you can ask it whether a method was called or how many times it was called. Typical mocks are classes with side effects that need to be examined, e.g. a class that sends emails or sends data to another external service.&lt;/li&gt;
&lt;li&gt;Learn mocking - &lt;a href="https://semaphoreci.com/community/tutorials/stubbing-and-mocking-with-mockito-2-and-junit"&gt;https://semaphoreci.com/community/tutorials/stubbing-and-mocking-with-mockito-2-and-junit&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Wrapper: something which hides implementation of some other code or internally uses other code. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;visit my personal blog: &lt;a href="https://rahilrehan.github.io"&gt;https://rahilrehan.github.io&lt;/a&gt;&lt;/p&gt;

</description>
      <category>testing</category>
    </item>
    <item>
      <title>Microservices concepts - Java</title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Sat, 09 Oct 2021 02:19:03 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/microservices-concepts-java-1a5a</link>
      <guid>https://dev.to/rahil_rehan/microservices-concepts-java-1a5a</guid>
      <description>&lt;h2&gt;
  
  
  Microservices Concepts (Using Java technologies)
&lt;/h2&gt;

&lt;p&gt;In microservices we can have multiple services running, and each service can have multiple instances as well.&lt;/p&gt;

&lt;p&gt;Product service: browse products using rest api&lt;br&gt;
Order service: places order&lt;br&gt;
Inventory service: checks if product is available in inventory&lt;br&gt;
Notification service: &lt;/p&gt;

&lt;h3&gt;
  
  
  Discovery Service
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Each service can have multiple instances running and they need to discover each other.&lt;/li&gt;
&lt;li&gt;As we cannot particularly specify the service as they are multiple instances, referring to different service needs to be generic.&lt;/li&gt;
&lt;li&gt;Referring to one service means to refer to available services.&lt;/li&gt;
&lt;li&gt;In java we can use spring cloud netflix eureka&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Centralized configuration
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If we change a config variable in code, we need to make change to code, compile and re-deploy it again.&lt;/li&gt;
&lt;li&gt;If we have multiple instances of same service then it becomes hard as we need to take down those services as well .&lt;/li&gt;
&lt;li&gt;Solution: Use a centralized config server&lt;/li&gt;
&lt;li&gt;Store all config variables of different services in different files in a git repository or local store.&lt;/li&gt;
&lt;li&gt;Whenever there is change in these files, config server will have new config variables.&lt;/li&gt;
&lt;li&gt;Now to refresh all the config variables in runtime, you need to call to some api actuator which will refresh the config variables in that service and all its instances.&lt;/li&gt;
&lt;li&gt;In Java Central config server with automatic refresh&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Storing secrets in vault
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;usernames, passwords, database links etc have to be hidden from configuration&lt;/li&gt;
&lt;li&gt;We have to install vault in our system or via docker, starting vault will start a server as well. &lt;/li&gt;
&lt;li&gt;Save all the secrets in vault.&lt;/li&gt;
&lt;li&gt;In Java we can use Spring starter vault config to use secrets in our code.&lt;/li&gt;
&lt;li&gt;It's enough just to connect to vault server and all the secrets are made available to the service.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Message Broker
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;One service might depend on other service, change in config variable of one service may effect the other.&lt;/li&gt;
&lt;li&gt;We can use message broker like RabbitMQ, which broadcast that there is some change in config to other services which are subscribed to that service which has changes. &lt;/li&gt;
&lt;li&gt;RabbitMQ must be installed on the host system and runs on some host and port.&lt;/li&gt;
&lt;li&gt;RabbitMQ in infra?&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  API gateway
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We may have different services in our application.&lt;/li&gt;
&lt;li&gt;Each service may handle different endpoints.&lt;/li&gt;
&lt;li&gt;API gateway, receives a uri request from clients.&lt;/li&gt;
&lt;li&gt;It redirects the client to correct service.&lt;/li&gt;
&lt;li&gt;It acts like a central endpoint distributer.&lt;/li&gt;
&lt;li&gt;We can attach load balancers to each service as well.&lt;/li&gt;
&lt;li&gt;Authentication, monitoring and rete limiting can also be taken care here.&lt;/li&gt;
&lt;li&gt;API gateway is also a service and should be present in discovery service.&lt;/li&gt;
&lt;li&gt;In Java, spring cloud gateway&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Sercuring server with authentication and authorization
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Run keycloak on host machine, it will start at some port&lt;/li&gt;
&lt;li&gt;token relay:

&lt;ul&gt;
&lt;li&gt;If authentication is done by token, using keycloak as we will be authorizing at api gateway, we need to send this token to the respective gateway as well. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;In Java, use Keycloak&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Resilience
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Order service asynchronously communicates with Inventory service&lt;/li&gt;
&lt;li&gt;Inventory service can go down, so we need to make it resilient&lt;/li&gt;
&lt;li&gt;RequestInterceptor:

&lt;ul&gt;
&lt;li&gt;When one service is speaking to other, authentication token is not sent. Because, it is not token relay mechanism.&lt;/li&gt;
&lt;li&gt;For passing auth token, we need to get the token and append it to authorization bearer in the request header and then send the request to service.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Definition: watch for service outages and network latencies and temporarily stop the service until service starts functioning normally again .&lt;/li&gt;
&lt;li&gt;In Java, Resilience4g or hystrix(netflix - not maintained right now)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Event driven microservices architecture
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When order is placed, order service broadcasts message to notification service via RabbitMQ. &lt;/li&gt;
&lt;li&gt;Use rabbitMQ binders to listen for events

&lt;ul&gt;
&lt;li&gt;output binders: to send notifications&lt;/li&gt;
&lt;li&gt;input binders: to receive notifications&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h3&gt;
  
  
  Distributed tracing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;microservices pattern  allows us to track the requests from one microservice to other.&lt;/li&gt;
&lt;li&gt;We give unique id along the journey of the request&lt;/li&gt;
&lt;li&gt;each request has same trace id along its journey&lt;/li&gt;
&lt;li&gt;but when there is a circuit breaker in between, trace id changes as circuit breaker spawns a new thread. But there is a workaround to keep our trace id same(traceableExecuterService in Java)&lt;/li&gt;
&lt;li&gt;In java, we use spring cloud sleuth and zipkin &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Centralized logging
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;logs are all over different services&lt;/li&gt;
&lt;li&gt;place all logs in a centralized manner &lt;/li&gt;
&lt;li&gt;First we need to use some logging library at each service like log4J or logback&lt;/li&gt;
&lt;li&gt;beware of security issues while logging&lt;/li&gt;
&lt;li&gt;In java (ELK stack is popular -&amp;gt; elastic search, logstash and kibana)

&lt;ul&gt;
&lt;li&gt;logstash: application services send logs to logstash over tcp. these logs are then sent from logstash to elastic search.

&lt;ul&gt;
&lt;li&gt;phases:   (configure in logstash.conf)

&lt;ul&gt;
&lt;li&gt;input: from services using tcp or ftp or rabbitMQ&lt;/li&gt;
&lt;li&gt;filter: filter logs&lt;/li&gt;
&lt;li&gt;output: send to elastic search&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Elastic search(search engine implementation in java): Used to store logs

&lt;ul&gt;
&lt;li&gt;Each service has its own index in elastic search&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Kibana: Visualize, Query and filter elastic search data&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  End
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Do not use microservice architecture unless there is a need. Monoliths are best in most cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Visit my personal blog here: &lt;a href="https://rahilrehan.github.io/"&gt;https://rahilrehan.github.io/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>java</category>
    </item>
    <item>
      <title>Javascript Notes, Part-06 - Classes, Objects, Prototypes.</title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Thu, 23 Apr 2020 13:03:58 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/javascript-notes-part-06-classes-objects-prototypes-3m45</link>
      <guid>https://dev.to/rahil_rehan/javascript-notes-part-06-classes-objects-prototypes-3m45</guid>
      <description>&lt;p&gt;Let's get some terminology aside first.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class is a template for what things and functionalities our objects should have.&lt;/li&gt;
&lt;li&gt;Objects are the real existing entities that have properties and functions(methods) attached to them.&lt;/li&gt;
&lt;li&gt;Factory functions, they create new entities as the name suggests. Example: Object.create().&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;.&lt;/em&gt; notations can be used to grab the property value of an object using a property name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In JS objects play a very huge role!&lt;/p&gt;

&lt;p&gt;Ok, let's dwell deep in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;userCreator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
 &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;};&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userCreator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Will&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userCreator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tim&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is defenitely not the code you will write for your applications but there are lot of lessons to learn here, let's see what's happening.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have created a new factory function userCreator which creates a new object and sets properties for given arguments.&lt;/li&gt;
&lt;li&gt;Here we create two objects, user1, and user2 by invoking the factory function userCreator.&lt;/li&gt;
&lt;li&gt;Think about this, in the last line we invoke the method increment on user1. Now, look at the code of increment in userCreator. We use newUser object which is not present in our global scope, then how can we still invoke increment. That is the magic of closure again!&lt;/li&gt;
&lt;li&gt;Assume, we have 1000 users. For each user, we have a copy of the increment method. Is our code DRY? No.(shortcoming)&lt;/li&gt;
&lt;li&gt;Assume you want to add a property to an object, you manually have to do it. (shortcoming)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prototype chaining
&lt;/h2&gt;

&lt;p&gt;Store the increment function in just one object and have the interpreter, if it&lt;br&gt;
doesn't find the function on user1, look up to that object to check if it's there.&lt;/p&gt;

&lt;p&gt;Link user1 and the object that has functions so the interpreter, on not finding .increment, makes sure to check up in that object where it would find it.&lt;/p&gt;

&lt;p&gt;Make the link with Object.create() technique&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;userCreator&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userFunctionStore&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userFunctionStore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="na"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;},&lt;/span&gt;
 &lt;span class="na"&gt;login&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Logged in&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userCreator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Will&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userCreator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tim&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Object.create(functionStore) automatically &lt;em&gt;references&lt;/em&gt; methods in functionStore to our objects.&lt;br&gt;
This way, we are not creating copies of the method increment.&lt;/p&gt;

&lt;p&gt;What's going on under the hood? let's see.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Object.create(userFunctionStore) is going to add reference value to &lt;em&gt;&lt;strong&gt;proto&lt;/strong&gt;&lt;/em&gt; property on the object(a hidden property, also read as &lt;em&gt;dunder proto dunder&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;So, when we call user.increment(), we will first look-up if the object has the given property. If we don't find it, JS doesn't panic instead it &lt;em&gt;looks up through the prototype chain&lt;/em&gt; to find the methods. This is JS's prototypal nature.&lt;/li&gt;
&lt;li&gt;Hence, we have removed the problem of having copies of methods for each object.&lt;/li&gt;
&lt;li&gt;Note the usage of &lt;em&gt;this&lt;/em&gt; keyword in the above code, which generalizes the user on which we are working on. &lt;em&gt;this&lt;/em&gt;(the current user) is an implicit parameter which is passed into &lt;em&gt;increment&lt;/em&gt; methods.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Inbuilt methods
&lt;/h2&gt;

&lt;p&gt;So, every object has &lt;strong&gt;proto&lt;/strong&gt; and every object is linked to one functionStore by default which is Object.prototype which has some interesting functions like hasOwnProperty. Object.prototype is on the top of the prototype chain(its &lt;strong&gt;proto&lt;/strong&gt;:null).&lt;/p&gt;

&lt;p&gt;Some notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using functions inside methods will have &lt;em&gt;this&lt;/em&gt; keyword set to global.&lt;/li&gt;
&lt;li&gt;To overcome this problem, use function.call(this) or use arrow functions which are lexically scoped.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  new keyword for cleaner code
&lt;/h2&gt;

&lt;p&gt;When we call the function that returns an object with new in front we automate 2&lt;br&gt;
things&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new user object&lt;/li&gt;
&lt;li&gt;Return the new user object&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We need to modify the factory function a little bit to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;userCreator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;userCreator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;userCreator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;Eva&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at how we set the object which had all the functions to userCreator function.  All functions have a default property &lt;em&gt;prototype&lt;/em&gt; which is initialized to an empty object, which is where we store our methods like increment.&lt;br&gt;
Now, every users &lt;strong&gt;proto&lt;/strong&gt; will be set to the userCreator.prototype.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understanding all of these concepts is pretty hard for new developers and hence classes were introduced.&lt;/li&gt;
&lt;li&gt;Normally, developers if not using class keyword then they would capitalize these factory functions just to let know the other developers that those factory function would need a new keyword before it.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  The class keyword
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;UserCreator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nx"&gt;login&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;UserCreator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Eva&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;It does the same thing as the new keyword. But creates a function and object combo. function part has the variable assignments and object part has method definitions.&lt;/p&gt;

&lt;p&gt;Let's look at the internals&lt;br&gt;
first, function part, the constructor&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;userCreator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then, sets prototype, remaining code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;userCreator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;userCreator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;login&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Therefore it is readable, bundled, clean to look at and looks like other languages(but the internals are very different!).&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Javascript Notes, Part-05 - Promises.</title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Thu, 23 Apr 2020 10:25:04 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/javascript-notes-part-05-promises-341o</link>
      <guid>https://dev.to/rahil_rehan/javascript-notes-part-05-promises-341o</guid>
      <description>&lt;p&gt;In the previous post, we have learned that there is no way to interact with browser functionalities like setTimeout. We don't know when they will end, we cannot return anything from those callback function. We just leave it up to JS to execute the code in those callbacks.&lt;/p&gt;

&lt;p&gt;Do you want to trust setTimeout(or any other browser functions) or would you prefer something more intricate which would &lt;em&gt;promise&lt;/em&gt; you that it would let you know when such browser functionalities end, exits and could also provide you a way to return values? &lt;/p&gt;

&lt;p&gt;In ES6 Promises were introduced. These facade functions start functionality in the browser and in JS memory as well so that we can track what's going on in the background.&lt;/p&gt;

&lt;p&gt;So, what do they do?&lt;/p&gt;

&lt;p&gt;When promise functions are invoked they start the functionality in the browser as well as store data in JS memory to keep track of what is going on.&lt;/p&gt;

&lt;p&gt;Let's look at an example using fetch(a browser facade function)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;futureData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://twitter.com/will/tweets/1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;futureData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;display&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Me first!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, let us see what happened.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Function &lt;em&gt;display&lt;/em&gt; is stored in global memory.&lt;/li&gt;
&lt;li&gt;fetch, which is browser function introduced in ES6 is invoked with URL.&lt;/li&gt;
&lt;li&gt;Even though the request to get data might take time, fetch immediately returns a JS object(Promise). This Promise object has fields {value:..., onfulfillment:[], onrejection:[]}&lt;/li&gt;
&lt;li&gt;This Promise object is stored in futureData.&lt;/li&gt;
&lt;li&gt;This Promise object stored in futureData has method &lt;em&gt;.then&lt;/em&gt; which takes a callback as an argument.&lt;/li&gt;
&lt;li&gt;The callback argument &lt;em&gt;display&lt;/em&gt; is stored in &lt;em&gt;onfulfillment&lt;/em&gt; array.&lt;/li&gt;
&lt;li&gt;The fetch works on its own to get data apart from JS, our Thread of Execution proceeds and prints "Me first!" on the console.&lt;/li&gt;
&lt;li&gt;Now, whenever our fetch fetches the data and returns a value, it is stored in the value property.&lt;/li&gt;
&lt;li&gt;Once the value property of Promise(futureData) is changed, the function &lt;em&gt;display&lt;/em&gt; stored in &lt;em&gt;onfulfillment&lt;/em&gt; array gets executed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, what have we achieved? &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A nice cleaner way to code things.&lt;/li&gt;
&lt;li&gt;A promise object to keep track of what is going on.&lt;/li&gt;
&lt;li&gt;And we can now return data to the caller.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Bonus!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When does promise deferred functions stored in &lt;em&gt;onfulfillment&lt;/em&gt; array are going to be executed? Immediately? No way, JS is synchronous. Remember that!&lt;/p&gt;

&lt;p&gt;Similar to the callback queue, we have a new queue for these new ES6 operations and that is called the &lt;em&gt;microtask queue&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Callback functions from the promise objects are put into the microtask queue.&lt;/li&gt;
&lt;li&gt;Our event loop now has a new job, that is to check the microtasks queue too.&lt;/li&gt;
&lt;li&gt;Microtask queue is given preference than the callback queue.&lt;/li&gt;
&lt;li&gt;Value property of Promise object is updated only after we execute the entire JS global code.&lt;/li&gt;
&lt;li&gt;There is also a .catch method attached to Promises which adds a parameter callback function to &lt;em&gt;onrejection&lt;/em&gt; array, these callbacks are executed when errors are returned.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;onfulfillment&lt;/em&gt; and &lt;em&gt;onrejection&lt;/em&gt; are hidden properties of Promise object.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Javascript Notes, Part-04 - Async JS, and Event Loop.</title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Thu, 23 Apr 2020 08:17:23 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/javascript-notes-part-04-async-js-and-event-loop-2i3l</link>
      <guid>https://dev.to/rahil_rehan/javascript-notes-part-04-async-js-and-event-loop-2i3l</guid>
      <description>&lt;p&gt;&lt;strong&gt;Some background first:&lt;/strong&gt;&lt;br&gt;
Assume while executing code line by line,  our (Thread of Execution)TOE meets with a code that runs really slow(maybe a server requests), then the code below that line has to wait because JS is synchronous language and it executes code line by line. This wait can sometimes take hours!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tweets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getTweets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://twitter.com/will/1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// ⛔350ms wait while a request is sent to Twitter HQ&lt;/span&gt;
&lt;span class="nx"&gt;displayTweets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tweets&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// more code to run&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I want to runnnn!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To solve this, we have to make our code asynchronous but note that JS itself is synchronous. Big challenge!&lt;/p&gt;

&lt;p&gt;This is where our browsers async features come into play.&lt;br&gt;
Browser is amalgamate of JS + console + dev tools + sockets + DOM + rendering engines + underlying language etc.&lt;/p&gt;

&lt;p&gt;Note that some features you think are JS are not even JS. For example, console, timeouts, XHR, DOM. These functionalities are provided by the browser.&lt;/p&gt;

&lt;p&gt;Now, the question is, Can we elevate these browser features to introduce the concept of Async in JS? Yes, We can.&lt;/p&gt;

&lt;p&gt;Let's look at an example, understand what is happening and what would happen in the snippet given below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;printHello&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;printHello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Me first!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Our function printHello gets stored in the global memory store.&lt;/li&gt;
&lt;li&gt;setTimeout(a browser function) is called with two arguments a function and time.&lt;/li&gt;
&lt;li&gt;Once the setTimeout function is invoked, our JS TOE proceeds to execute the code.&lt;/li&gt;
&lt;li&gt;We now print "Me first" to the console.&lt;/li&gt;
&lt;li&gt;setTimeout function starts a timer in the browser and once the timer is complete, the parameter function is put into something called the callback queue.&lt;/li&gt;
&lt;li&gt;Now, there is one more thing called the event loop which checks if any function is currently on JS call stack or there is any global code that has to be executed.&lt;/li&gt;
&lt;li&gt;When there is nothing in the call stack and no global code, we put the callback function from the callback queue into the call stack, put parenthesis at the end of function hence invoking the function.&lt;/li&gt;
&lt;li&gt;Therefore, our callback functions which are passed into setTimeout will get executed only when there is nothing on the call stack and no remaining global code.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To introduce async concepts in our code we use browser features.&lt;/li&gt;
&lt;li&gt;Any callback function we pass into the browser functions gets executed only after there are no functions on the call stack and there is no global code yet to run.&lt;/li&gt;
&lt;li&gt;Event loop is what which infinitely checks call stack, global code, and call back queue.&lt;/li&gt;
&lt;li&gt;Note that even though we use setTimeout(printHello, 0) in the above example printHello will get executed in the end.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Alright! looks good, is there anything else? Oh yes, there is!&lt;/p&gt;

&lt;p&gt;There are problems.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You do many things using browser functionalities(like setTimeout) but we don't have anything to track stuff going on in the browser.&lt;/li&gt;
&lt;li&gt;We can't return a value from the function which is passed into browser functionalities, and hence the problem of callback hell.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will learn ways to solve these problems in further posts.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Javascript Notes, Part-03 - Closures</title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Thu, 23 Apr 2020 03:10:02 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/javascript-notes-part-03-closures-3j5k</link>
      <guid>https://dev.to/rahil_rehan/javascript-notes-part-03-closures-3j5k</guid>
      <description>&lt;p&gt;YOU CAN RETURN A FUNCTION FROM A FUNCTION!&lt;/p&gt;

&lt;p&gt;This is possible because functions are first-class objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;createFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;multiplyBy2&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;multiplyBy2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;generatedFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;generatedFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returning a function, storing it in a variable(sorry, I mean const) and invoking the returned function from outside where it is defined. Well, Looks great!!&lt;/p&gt;

&lt;p&gt;But wait! what if our returned function is using data of its parent function? Look at the below example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;outer&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
 &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;incrementCounter&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;incrementCounter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myNewFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;myNewFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;myNewFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, when our returned function is invoked we try to access a variable that is already dead! I mean, trying to access a variable from a function that already got executed, popped out of call stack, and whose local memory is cleared. There is no way of accessing that variable right?&lt;/p&gt;

&lt;p&gt;But, this is JS! anything can happen, lol.&lt;br&gt;
Okay! let's understand what is happening.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Attention!&lt;/strong&gt;  &lt;em&gt;When a function is returned a datastore is created if the outer function has any data which is being referenced by inner function(returned fn). This datastore is called closure. I usually call it a backpack&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One more time:&lt;/strong&gt; &lt;em&gt;A closure is a combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.&lt;/em&gt;[MDN]&lt;/p&gt;

&lt;p&gt;Note that the data inside a closure is persistent and referred.&lt;/p&gt;

&lt;p&gt;One more takeaway! See below example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;outer&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
 &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;incrementCounter&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
 &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;incrementCounter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myNewFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;myNewFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;myNewFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;anotherFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;anotherFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;anotherFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we create two different backpacks(closures) for two variables myNewFunction and anotherFunction. This happens because we have two different execution contexts while assigning returned functions to variables.&lt;/p&gt;

&lt;p&gt;Application of closures: Asynchronous JS, Iterators and generators, and many more.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Javascript Notes, Part-02 - Callbacks and Higher-order functions</title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Wed, 22 Apr 2020 15:07:28 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/javascript-notes-part-02-callbacks-and-higher-order-functions-3m0n</link>
      <guid>https://dev.to/rahil_rehan/javascript-notes-part-02-callbacks-and-higher-order-functions-3m0n</guid>
      <description>&lt;p&gt;Remember one word when you write code, "DRY" &lt;em&gt;(Don't Repeat Yourself)&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Higher-order functions and callbacks.
&lt;/h2&gt;

&lt;p&gt;Most of the code can be optimized, shortened, and modularized while writing functions. If you can modularly write code that doesn't repeat the same functionality then you are following the DRY principle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some DRY principles
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Generalization:&lt;/strong&gt; First try to generalize your functions. If two functions do the same things then probably they can be combined to one.&lt;br&gt;
 Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;nineSquared&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;tenSquared&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//above code is not DRY&lt;/span&gt;

 &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;numSquared&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//generalized function&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Functions are first-class objects&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They can be assigned to variables.&lt;/li&gt;
&lt;li&gt;They can be passed as arguments.&lt;/li&gt;
&lt;li&gt;Can be returned from other functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Higher-order functions:&lt;/strong&gt; A function that takes in a function as an argument or returns out a function.&lt;br&gt;
&lt;strong&gt;Callback functions:&lt;/strong&gt; Functions that are passed in as parameters to higher-order functions are called as callback functions.&lt;/p&gt;

&lt;p&gt;We can use a callback function to DRY our code. Look at the below example where multiplyBy2 function acts as a callback function. Note that you can pass in &lt;br&gt;
 different functions as a callback function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;copyArrayAndAdd3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
 &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;copyArrayAndAdd3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//we are breaking the DRY principle in above&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;copyArrayAndManipulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;instructions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
 &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;instructions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;multiplyBy2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;copyArrayAndManipulate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;multiplyBy2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//Here multipleBy2 is passed as callback function to higher-order function copyArrayAndManipulate.&lt;/span&gt;
&lt;span class="c1"&gt;//This callback function can be later invoked inside the parent function.&lt;/span&gt;
&lt;span class="c1"&gt;// multipleBy2 can be replaced with other functions, hence our code is DRY!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Some Notes:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Async JS: callbacks are the core aspect of async JS&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parameters are passed in as a link back to where it was saved in global, We are not copying but sending the reference of the variable into a function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Blocks like for loop get their own protected namespace but not an entire execution context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Methods, They can be passed as inputs, as arguments into other functions, they reference back to where they were born, more on this later.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Javascript Notes, Part-01 - Principles of JS </title>
      <dc:creator>Rahil Rehan</dc:creator>
      <pubDate>Wed, 22 Apr 2020 06:52:43 +0000</pubDate>
      <link>https://dev.to/rahil_rehan/javascript-notes-principles-of-js-part-01-23cj</link>
      <guid>https://dev.to/rahil_rehan/javascript-notes-principles-of-js-part-01-23cj</guid>
      <description>&lt;p&gt;There are five big areas of JS which are foundational pieces to understand JavaScript in-depth:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Principles of JavaScript&lt;/li&gt;
&lt;li&gt;Callbacks &amp;amp; Higher-order functions&lt;/li&gt;
&lt;li&gt;Closures&lt;/li&gt;
&lt;li&gt;Asynchronous JavaScript &amp;amp; the event loop&lt;/li&gt;
&lt;li&gt;Classes &amp;amp; Prototypes (OOP)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this blog let's talk about Principles of JavaScript. These principles are basic building blocks that you need to remember all the time. These will be foundations for understanding complex concepts later and will also help you in effective technical communication.&lt;/p&gt;

&lt;h2&gt;
  
  
  Principles of JavaScript
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Thread of Execution
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;JS is a single-threaded programming language hence only one thread handles the code execution, therefore the code is executed line by line known as the &lt;strong&gt;thread
of execution(TOE)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Right now with one thread and without complex code, JS has a &lt;strong&gt;synchronous&lt;/strong&gt; programming style.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Execution Context
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Then there is something called &lt;strong&gt;execution context(EC)&lt;/strong&gt; which is an abstract concept that holds information about the environment within which the current code is being executed. Some notes on EC&lt;/li&gt;
&lt;li&gt;easy definition: In the execution context, we go and compartmentalize any data we're storing while we're inside that function&lt;/li&gt;
&lt;li&gt;There are two main components in EC, they are 

&lt;ul&gt;
&lt;li&gt;A Thread of Execution(TOE)&lt;/li&gt;
&lt;li&gt;Memory (A new memory store for each EC)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Global EC is created before any code is executed.&lt;/li&gt;
&lt;li&gt;A new function always creates a new EC.&lt;/li&gt;
&lt;li&gt;With a new EC, a TOE is now in that function and also a new local memory store(where all data related to function are stored) is created for each new function.&lt;/li&gt;
&lt;li&gt;When TOE completes in function, it is cleared from memory except for a returned value from the function.&lt;/li&gt;
&lt;li&gt;There is always one TOE alive until the program is exited.&lt;/li&gt;
&lt;li&gt;JavaScript keeps track of the function which it is currently running by using a call stack.&lt;/li&gt;
&lt;li&gt;TOE is always in the function which is on top of the stack.&lt;/li&gt;
&lt;li&gt;There is always a global context on the call stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Functions and call stack
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript keeps track of what function is currently running using a call stack.&lt;/li&gt;
&lt;li&gt;In JS code, i.e in JS engine, a new function means a new execution context is created and when we call that that function using parenthesis '()' we push that function onto the call stack then our TOE runs the code in the topmost function of the call stack.&lt;/li&gt;
&lt;li&gt;When finished running the function we remove it from the call stack and TOE is also returned to the parent function.&lt;/li&gt;
&lt;li&gt;As call stack is a stack we have the functionality of LIFO(Last In First Out), we execute functions which are most recent.&lt;/li&gt;
&lt;li&gt;Therefore there is a chance of code getting blocked for a longer period of time in the parent function(a function calling another function) when the child function(a function which is being called) takes longer time to be executed and return the TOE to the parent function.&lt;/li&gt;
&lt;/ul&gt;

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