<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Niroshan Krishnamoorthy on Medium]]></title>
        <description><![CDATA[Stories by Niroshan Krishnamoorthy on Medium]]></description>
        <link>https://medium.com/@niroshantrinity?source=rss-c19181ceaed2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*9NsStWLSm-E4aUz8</url>
            <title>Stories by Niroshan Krishnamoorthy on Medium</title>
            <link>https://medium.com/@niroshantrinity?source=rss-c19181ceaed2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 09:31:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@niroshantrinity/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Connect MongoDB to your NodeJS project using mongoose.]]></title>
            <link>https://medium.com/@niroshantrinity/connect-mongodb-to-your-nodejs-project-using-mongoose-cf72fc356495?source=rss-c19181ceaed2------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf72fc356495</guid>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Niroshan Krishnamoorthy]]></dc:creator>
            <pubDate>Sun, 03 May 2020 12:12:36 GMT</pubDate>
            <atom:updated>2020-05-03T12:12:36.697Z</atom:updated>
            <content:encoded><![CDATA[<p>Mongoose is a MongoDB object modelling tool. We mainly model the objects of MongoDB into a document or instance of an object. This tool works in an asynchronous way which means data management is done parallel without affecting the flow of the application. With the use of this library we can query, update and manipulate with our MongoDB database in our NodeJS app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*iDvsmUwzZQxJSKdL0xzwIA.png" /></figure><p>To get MongoDB into your project we need to install it from NPM package manager.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*Myjc75VqPYV-q6IeO1gnvQ.png" /></figure><p>Next we need to require the downloaded package in to our file(App.js) and open connection to the database in your local machine or in the cloud.</p><p>On the below code snippet we have used MongoDB Atlas, a platform to host your MongoDB in the cloud. Check about MongoDB Atlas <a href="https://www.mongodb.com/cloud/atlas"><em>Here</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AWpR27Hic0qjN93Z8r75uw.png" /></figure><p>I have defined the connection string on the “ATLAS_URI” environment variable.</p><p>We can also connect to the local DB with the help of below method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*waLnLLNX7aY2VRUBUnBEfw.png" /></figure><p>We have a pending connection on the above two methods we need to confirm whether our connection is successful. For that,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*SOj0aup4BBXjesVZzpkGww.png" /></figure><p>Once the connection is open we can start making the schema since everything in mongoose is defined with schema.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1BxzQ_z45q6W6Mw-TFLhww.png" /></figure><p>Our Schema is referenced as userSchema, now we need to compile our schema into a Model for this we use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uAck3566XywVdFIqTp9vdQ.png" /></figure><p>Models are the classes with which we can construct documents. In this case each document will be a <strong>User </strong>with it’s properties “username”.</p><p>Let’s create the first user document for our model</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*VqZn6rwwHxAcFTFt5IVtqw.png" /></figure><p>Even though we have created our first document we have not saved in our database, for this we use <strong>save</strong> method. This will return a promise. We can handle the errors too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*Rz-LVyWBNBv5c4aJwFwg5w.png" /></figure><p>With help of models we can perform queries on them. For Example, to get all the user documents in the User model we can use <strong>find</strong> method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e1XGtChcCOO6Kp7kre3zpQ.png" /></figure><p>You can refer to other built in query methods of mongoose <a href="https://mongoosejs.com/docs/queries.html"><em>here</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf72fc356495" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[REST Constraints]]></title>
            <link>https://medium.com/@niroshantrinity/rest-constraints-6f5885bc48a0?source=rss-c19181ceaed2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f5885bc48a0</guid>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[backend-development]]></category>
            <dc:creator><![CDATA[Niroshan Krishnamoorthy]]></dc:creator>
            <pubDate>Tue, 31 Mar 2020 11:35:12 GMT</pubDate>
            <atom:updated>2020-03-31T11:35:12.244Z</atom:updated>
            <content:encoded><![CDATA[<p>REST stands for Representational State Transfer. This is an architecture used in order to maintain communication over the network. REST architecture mainly has two components they are REST Clients and REST Servers</p><p>RESTful Services mainly use the TCP/IP connection for message passing. These messages follow HTTP protocols. But REST specially has its own constraints when using HTTP messages. These constraints are</p><p>· Client-Server</p><p>· Stateless</p><p>· Cache</p><p>· Uniform-Interface</p><p>· Layered System</p><p>· Code on Demand</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*wX_Y9RJ10sNp4lIR60T3Bw.png" /></figure><p><strong>Client-Server</strong></p><p>This constraint says that the REST application should have a client server architecture. Under this constraint it states that the client requests a resource while the server store and responds with a resource. This connection is decoupled which means the client doesn’t know the business logic used by the server for accessing the resource. And the Servers don’t know the frontend UI of the client.</p><p><strong>Stateless</strong></p><p>This constraint says that the server shouldn’t store the session related data of the client(state), where as the client is free to store any state related data. The communication between the client and server is stateless. Hence all the information of a request is within the request. Therefore, every time the client requests a resource from the server it seems like the first request to the server by the client.</p><p><strong>Cache</strong></p><p>This constraint states responses should be cacheable, if possible. Therefore client will not need to make additional request to server to fetch the same data which improves network optimization.</p><p><strong>Uniform interface</strong></p><p>This constraint states that the request from any type of client (mobile, laptop, MAC) should always interact in the same way. This provides a generality as all the components interact in the same way. This is the main key constraint which differentiate REST and Non-REST API.</p><p><strong>Layered-System</strong></p><p>This constraint states that the architectures to be composed of hierarchical layers. Each layer will not know anything beyond its immediate layer. This limits the amount of complexity that can be introduced at any layer.</p><p><strong>Code on Demand</strong></p><p>This is an optional constraint out of all six constraints. This constraint states that, the server can provide executable code to the client in addition to the data. Hence this can create some security threats to the application. Therefore, this is made optional.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f5885bc48a0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Component Life Cycle methods in React]]></title>
            <link>https://medium.com/@niroshantrinity/component-life-cycle-methods-in-react-7654c3f6c20a?source=rss-c19181ceaed2------2</link>
            <guid isPermaLink="false">https://medium.com/p/7654c3f6c20a</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ui]]></category>
            <dc:creator><![CDATA[Niroshan Krishnamoorthy]]></dc:creator>
            <pubDate>Sun, 22 Mar 2020 16:27:33 GMT</pubDate>
            <atom:updated>2020-03-22T16:27:33.661Z</atom:updated>
            <content:encoded><![CDATA[<p>React Component life cycles methods can be categorized according to 3 phases.</p><p>They are namely Mounting,Updating and Unmounting</p><p>We use some life cycle methods which comes under these phases. These methods are automatically triggered when a component is created and used. We can override these methods in our code to perform special tasks within the application. For example we can use componentDidMount() method to perform AJAX calls using a component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CfaYxw2J1ysUZlPOZ_ecDA.jpeg" /></figure><p><strong>Mounting phase</strong></p><p>When we consider Mounting phase there are 3 methods we should discuss. They are</p><p>· getDerivedStateFromProps()</p><p>· render()</p><p>· componentDidMount()</p><p>But apart from these methods we need to discuss about the constructor.</p><p><strong>Constructor</strong></p><p>This is the function which is called at first when a component is mounted. This method is mainly used if the component needs to initialize the state of the component or Bind event handlers. If we are not using states inside a component we can use stateless components which will not have constructors, This will increase the overall performance of the application.</p><p>There are some things we should not do inside a constructor. That is, we should not cause side effects like requesting HTTP requests.</p><p>When calling the constructor it is mandatory to call the base class constructor using <em>super(props). </em>Only using this call we can access the props of that component. The next important point to note is that we need to use <em>this.state </em>when<em> </em>initializing the state instead of using <em>this.setState()</em></p><p><strong>getDerivedStateFromProps(props,state)</strong></p><p>This is a static method which means we can’t use <em>this</em> keyword inside this method. This a rarely used method out of other life cycle methods. This only used when state of the component depends on change over time. We should not cause any side effects in this method.</p><p><strong>render()</strong></p><p>This is the only required method in components. In this method we use <em>this.props</em> and <em>this.state </em>and return the JSX which creates the component. This is a pure function which means this will render the same UI for given props and state.</p><p>Inside this method it is not advised to change the state of the component which will cause re-rendering of the same component again. We should also not make AJAX calls inside this method.</p><p><strong>componentDidMount()</strong></p><p>This method is only called once inside a component’s life cycle. This method is called once a component and its children components are rendered. This is the perfect place to cause side effects like interacting with the DOM and making AJAX calls to load data.</p><p><strong>Updating phase</strong></p><p>There are 5 methods which gets triggered under this phase. They are</p><p>· getDerivedStateFromProps()</p><p>· shouldComponentUpdate()</p><p>· render()</p><p>· getSnapshotBeforeUpdate()</p><p>· componentDidUpdate()</p><p><strong>getDerivedStateFromProps(nextProps,nextState)</strong></p><p>This method is called whenever the component is about to re-render. This has the props and state as its arguments which causes the component to re-render.</p><p><strong>shouldComponentUpdate(nextProps,nextState)</strong></p><p>This methods dictates whether a component should re-render or not. This method is mainly used for performance optimization of the application. We should not cause side effects or call <em>setState() </em>method inside this method.</p><p><strong>render()</strong></p><p>This method is again triggered when the new props associate with the component or there is a change in the state of the component.</p><p><strong>getSnapshotBeforeUpdate(prevProps,prevState)</strong></p><p>This method is called right before the changes from the virtual DOM are reflected on the real DOM. This method returns null or an object which is used in the next method we will discuss.</p><p><strong>componentDidUpdate(prevProps,prevState,snapshot)</strong></p><p>This method is called after the rendering is finished in the re-rendering cycle. As in the mounting phase we can use this function to cause side effects in the updating phase.</p><p><strong>Unmounting Phase</strong></p><p>This phase has only one method which is called <strong>componentWillUnmount()</strong>. This function is called just before the component is unmounted and destroyed from the DOM. Inside this method we can perform cancelling network requests, removing event handlers and cancelling subscriptions. We should not call <em>setState()</em> method because this will not execute when a component is destroyed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7654c3f6c20a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Single Page Applications vs ReactJS]]></title>
            <link>https://medium.com/@niroshantrinity/single-page-applications-vs-reactjs-83d1e2fad78f?source=rss-c19181ceaed2------2</link>
            <guid isPermaLink="false">https://medium.com/p/83d1e2fad78f</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[single-page-applications]]></category>
            <dc:creator><![CDATA[Niroshan Krishnamoorthy]]></dc:creator>
            <pubDate>Sat, 21 Mar 2020 12:31:49 GMT</pubDate>
            <atom:updated>2020-03-21T12:31:49.535Z</atom:updated>
            <content:encoded><![CDATA[<p>React is a simple library that makes building User Interface Easier. But to understand it we need to talk about Single page applications.</p><p>It has the same idea as its name, it is an application which uses a single page to interact with its users. They load all the HTML, CSS and JavaScript when the page loads at first. Then different parts of HTML and CSS get shown, hidden, deleted, as well as underlying data. The User Interfaces are manipulated according to the data that is being interacted through the application. This is mainly because of the use of AJAX technology. Without AJAX this will never happen.</p><p>Facebook is a single page application because most of the structure is loaded the first time we visit the site. As we move around different pages it doesn’t need to ask the server what HTML, CSS and JS we need, it just need the data to fill in the blanks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4MyhkJ6VfHDCMlTf" /><figcaption>Photo by <a href="https://unsplash.com/@konkarampelas?utm_source=medium&amp;utm_medium=referral">Kon Karampelas</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The concept of using different data on the same code base is known as <strong>composition</strong>. The set of code can get reused and mixed together to compose one or many pages. React call this set of code components, and these components are already being built by many other developers where we just need to use them by downloading it from npm.</p><p>Therefore, ReactJS is used as a Library for creating UI’s for Single-Page Applications which uses large amount of data. Similarly, there are other frameworks which act in the same way. Some of them are AngularJS and VueJS.</p><p>Let’s talk about ReactJS,</p><p>React behaves according to single source of truth concept. That is data is originated and kept in one place. And it has a one way data flow where the data can flow form parent component to child component. React uses state and props for data management. Every container (parent component) has it own state. To send information about its state to its’s children components it uses props. Props are read by the children and they behave according to their parent’s state.</p><p>For changes in the UI react uses something called a Virtual DOM which is like the Real DOM, Virtual DOM is a direct clone of the real DOM, but its much faster than the real DOM. Therefore if there is a change then the faster Virtual DOM is changed first and the react engine checks for any changes with the real DOM and Virtual DOM. And if it finds out a change then it only alters the changed part of virtual DOM with the Real DOM. This makes react applications run faster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*nN6fPJpA2eKfj9mg-3HBYQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83d1e2fad78f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is ELK Stack?]]></title>
            <link>https://medium.com/@niroshantrinity/what-is-elk-stack-547cc5af0389?source=rss-c19181ceaed2------2</link>
            <guid isPermaLink="false">https://medium.com/p/547cc5af0389</guid>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Niroshan Krishnamoorthy]]></dc:creator>
            <pubDate>Wed, 26 Feb 2020 06:11:06 GMT</pubDate>
            <atom:updated>2020-02-26T07:07:54.033Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*GaEqhtHAjPHDA3yKXqmq5A.jpeg" /></figure><p><strong>Purpose of Log Analysis.</strong></p><p>For cloud-based environment performance and infrastructure is very important. The performance of a cloud environment may get affected due to various loads, environment and number of active users. Therefore, reliability and node failure can become significant issues in cloud virtual machines.</p><p>A log management platform can monitor the above-mentioned issues and process operating system logs, NGINX, IIS server log for web traffic analysis, application logs, and logs on AWS (Amazon web services).</p><p>Log management is important for DevOps engineers and System Admins to make better decision. Hence Log management tools like ELK stack plays a major role in technical world.</p><p><strong>ELK (Elastic Search-Logstash-Kibana)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/305/1*US6MedlVy9OADYE5mV8smg.png" /></figure><p>ELK: Stack of components that is being released by Elastic (Company) and it used in Deployment.</p><p><strong>Elastic Search</strong>: It is a Distributed, NoSQL database with JSON like messages. Elastic Search can handle RESTful APIS. Therefore, if we have an instance of Elastic Search, we can access RESTful API using elastic Search instance. This offers Simple Deployment, Maximum Reliability and Easy Management. It is helpful for executing a quick search of the documents.</p><p>Elastic Search can store, search and analyze large volume of data. Elastic Search is being used as one of the common underlying search engines in modern web and mobile applications. Apart from its quick search facility it also offers many advance features.</p><p>Features of Elastic Search:</p><ul><li>Open source search server is written using Java</li><li>Used to index any kind of heterogeneous data</li><li>Has REST API web-interface with JSON output</li><li>Full-Text Search</li><li>Near Real Time (NRT) search</li><li>Sharded, replicated searchable, JSON document store</li><li>Schema-free, REST &amp; JSON based distributed document store</li><li>Multi-language &amp; Geolocation support</li></ul><p>Advantages of Elastic Search</p><ul><li>Store schema-less data and also creates a schema for your data</li><li>Manipulate your data record by record with the help of Multi-document APIs</li><li>Perform filtering and querying your data for insights</li><li>Based on Apache Lucene and provides RESTful API</li><li>Provides horizontal scalability, reliability, and multitenant capability for real time use of indexing to make it faster search</li><li>Helps you to scale vertically and horizontally</li></ul><p><strong>Logstash (Framework)</strong>: It is a collector which collects data from sources and streams them using a pipeline to the Elastic Search Instance. Here Elastic Search acts as a Database and Logstash acts as a streaming component which pushes the logs into the elastic search database. It acquires data from separate logs and normalize the data into your desired location.</p><p>This Contains 3 components</p><p>· <strong>Input:</strong> passing to process the logs into machine understandable code.</p><p>· <strong>Filters: </strong>Conditions used to perform particular action to the data.</p><p>· <strong>Output:</strong> Decision makers for processed logs.</p><p>Features of Logstash</p><ul><li>Events are passed through each phase using internal queues</li><li>Allows different inputs for your logs</li><li>Filtering/parsing for your logs</li></ul><p>Advantage of Logstash</p><ul><li>Offers centralize the data processing</li><li>It analyzes a large variety of structured/unstructured data and events</li><li>Offers plugins to connect with various types of input sources and platforms</li></ul><p><strong>Kibana</strong>: It is the UI component that helps us display the data as we wanted it to be over the elastic Search instance. It displays diagrams, geospatial data and graphs to understand the complex queries that is being performed on the elastic Search component.</p><p>Features of Kibana:</p><ul><li>Powerful front-end dashboard which is capable of visualizing indexed information from the elastic cluster</li><li>Enables real-time search of indexed information</li><li>You can search, View, and interact with data stored in Elasticsearch</li><li>Execute queries on data &amp; visualize results in charts, tables, and maps</li><li>Configurable dashboard to slice and dice logstash logs in Elastic Search</li><li>Capable of providing historical data in the form of graphs, charts, etc.</li><li>Real-time dashboards which is easily configurable</li><li>Enables real-time search of indexed information</li></ul><p><strong>Advantages and Disadvantages of Kibana</strong></p><ul><li>Easy visualizing</li><li>Fully integrated with Elasticsearch</li><li>Visualization tool</li><li>Offers real-time analysis, charting, summarization, and debugging capabilities</li><li>Provides instinctive and user-friendly interface</li><li>Allows sharing of snapshots of the logs searched through</li><li>Permits saving the dashboard and managing multiple dashboards</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*jn1H7S7xifP9o1l8goqYpw.png" /></figure><p><strong>Many companies like Netflix, LinkedIn, Tripwire, Medium uses ELK stack in their business.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=547cc5af0389" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript a Single threaded, Synchronous and non-blocking programming language.]]></title>
            <link>https://medium.com/@niroshantrinity/javascript-a-single-threaded-synchronous-and-non-blocking-programming-language-850e0e5d07d2?source=rss-c19181ceaed2------2</link>
            <guid isPermaLink="false">https://medium.com/p/850e0e5d07d2</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Niroshan Krishnamoorthy]]></dc:creator>
            <pubDate>Wed, 19 Feb 2020 09:20:12 GMT</pubDate>
            <atom:updated>2020-02-19T09:20:12.009Z</atom:updated>
            <content:encoded><![CDATA[<p>First of all, to understand this topic we should know what a program is. <em>A program has its own memory and scripts (Parse and execution of code)</em>.</p><p>Now we shall go into the big picture JavaScript. How JavaScript works.</p><p>As web-based programming language JavaScript runs in its own engine inside a browser. For example, in Chrome browser JavaScript runs in <strong>V8 </strong>engine, in Mozilla it runs in <strong>SpiderMonkey</strong> engine likewise different platforms have different engines. A JavaScript Engine reads the instructions we write and change them into machine readable instructions which can be read by the browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*bp2iAHjb7fcFF4Zp_SfP9A.jpeg" /></figure><p><strong>Google V8 engine</strong></p><p>When we consider the V8 engine it has 2 components the MEMORY HEAP and CALL STACK. Inside the memory heap the memory allocation happens and inside the call stack the code is read and executed. The variables and functions are stored in the memory and sent into the call stack.</p><p><strong>Call Stack</strong></p><p>How the call stack functions?</p><p>Call Stack is a stack data structure in nature which means it works according to First in Last Out principle. Let’s assume a program has 3 lines. The first line gets pushed into the stack executes and pops out from the stack and then the next line of code gets pushed to the stack. Like wise line by line the code gets called into the stack, executes and exits the stack</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/371/1*HuD_4eDke-xVu3Jsx-qREQ.png" /></figure><p>The output will be 1,2 and 3. This is what <strong>synchronous</strong> programming means.</p><p>Call stacks are the main part when we talk about the threads. JavaScript only consists of one call Stack hence it is called single threaded. There are other languages with multiple call stacks which are known as multi-threaded languages. As a result of JavaScript having one thread it is easy to compile and run programs in the browser where browser is responsible only in thinking about executing one single task at a time. Any how with the advantages of synchronous nature there comes a huge draw back in this type of programming <strong>that is blocking</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rQWd2p9dNbIFEyC-" /><figcaption>Photo by <a href="https://unsplash.com/@acfb5071?utm_source=medium&amp;utm_medium=referral">Keith Johnston</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When we consider a program with big task in between two simple tasks, the program executes the 1st simple task and waits until the Second task is completed to execute the final simple task. If this happens inside the browser the users have to wait for a long time to load a complex web application. If this had happened then JavaScript wouldn’t survived in this business. But JavaScript overcomes this blocking nature.</p><p><strong>JavaScript is Non-blocking</strong></p><p>JavaScript uses call backs and promises to overcome the blocking nature. To clearly see this, we should understand the JavaScript runtime environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*s8PDSxizAIpw4B6Tdtd_Rg.png" /><figcaption>JS runtime environment</figcaption></figure><p>JavaScript run time environment consists of the JavaScript engine, Web APIs, Callback queue and an Event loop.</p><p>To understand this further let’s look into a simple program.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*L1q09uL8KOXvZqFKuH5M3A.png" /></figure><p>The output of the above will be 1, 3, and then 2. Here the program doesn’t wait until the setTimeout() function is completed it automatically switches to the next command and runs it. Congratulations we have achieved <strong>asynchronous</strong> <strong>programming</strong> in a synchronous programming language. How does this happen?</p><p>Lets assume console.log(“1”) is <em>command 1</em> setTimeout() as <em>command 2 </em>and console.log(‘’3’’) as <em>command 3.</em></p><p>First, <em>command 1</em> is pushed, executes and pops out of the call stack and <em>command 2</em> gets pushed into the call stack and call stack identifies this function as a web API it pops it out and adds it to the web API region of the JS runtime environment. Then the Web API starts a timer for 2 and holds this function seconds during this time call stack will not have any commands hence the JS engine pushes the <em>command 3 </em>into the call stack and executes it and pops it out.</p><p>OK now the 1st and last command have executed. After the timeout of 2 seconds the JS runtime environment check if there is something inside the setTimeout() function and according to above program we can see that console.log(“3”) lies within the function. Hence JS runtime environment identifies that there is a <strong>Call back</strong> function to be executed then it pushes this Call back function into the call back queue region of the JS runtime environment.</p><p>Then the <strong>Event loop</strong> checks if the call stack is empty, and if the call stack is empty it checks if there are any call back functions available, if there is a call back function then it adds it to the call stack and executes it. At this point the call stack will be like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/205/1*dTy6Y9fTBzVCxf6hlBCgRQ.png" /><figcaption>view of call stack after callback() function execution</figcaption></figure><p>This is because the call back function contains the <em>console.log(“2”)</em> statement within it. Therefore 1st the <em>console.log(“2”)</em> statement gets executed, pops out of the stack and then callback() pops out.</p><p>This is how JavaScript works.</p><p>Sometimes Nested callbacks pass into sequence of async tasks. This is a mess and it is called “callback hell”. To overcome this problem JS has promise objects. JS has promise objects within it so that it can use them instead of call back functions. These objects have several properties, methods and mechanisms of chaining to handle complex async tasks.</p><p>Finally let’s come to a real-life example on what I mentioned above. Assume that you want to get an answer for a question from the teacher. For this you may call the teacher from the mobile, wait until she picks it up and you ask the question and get the answer while you are doing this, you can’t do any other work.</p><p>But what if you texted her. You will receive the answer but you can also do other work until she replies you back. This is what JS does to make itself <strong>asynchronous</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=850e0e5d07d2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[S.O.L.I.D Principles and OOP]]></title>
            <link>https://medium.com/@niroshantrinity/s-o-l-i-d-principles-and-oop-327def5b8a52?source=rss-c19181ceaed2------2</link>
            <guid isPermaLink="false">https://medium.com/p/327def5b8a52</guid>
            <category><![CDATA[oop-concepts]]></category>
            <dc:creator><![CDATA[Niroshan Krishnamoorthy]]></dc:creator>
            <pubDate>Tue, 18 Feb 2020 07:52:50 GMT</pubDate>
            <atom:updated>2020-02-18T07:52:50.619Z</atom:updated>
            <content:encoded><![CDATA[<p>This is one of the main principles that needed to be followed when developing software systems in industrial scale. This helps the developers to organize code in such way that they can review them later in the deployment stage or fixing bugs. This makes the source code simple so that a new developer can work on the existing project without any hesitations.</p><p>What does S.O.L.I.D mean?</p><p><strong>S: Single Responsibility Principle</strong></p><p><strong>O: Open/Close Principle</strong></p><p><strong>L: Liskov Substitution Principle</strong></p><p><strong>I: Interface Segregation Principle</strong></p><p><strong>D: Dependency Inversion Principle</strong></p><p><strong>Single Responsibility Principle</strong></p><p>This principle states that “The class should have one and only one reason to change”. What does this mean?</p><p>A single class should contain only one responsibility within it. This doesn’t mean that it should have only one method or property in a class it can contain any number of properties and methods where all of these properties and methods lead to a single responsibility.</p><p>This makes software implementation process simple and easy to understand, as a result developers can update the software without a burden.</p><p>Single Responsibility Principal is mildly focusing on the Abstraction concept of OOP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*XdICx_i16rTwHdbIr0shrA.jpeg" /></figure><p><strong>Open/Close Principle</strong></p><p>Here the principle states “Software entities should be open for extension but closed for modifications”. That means when a new class is implemented it should be always ready to accept updated functionalities from the client. Even if the class needs to update its functionality the code cannot be changed. Then how to solve this problem? This is where the role of abstraction comes in handy. Here we can use the abstraction concepts in updating a class with its functionalities.</p><p>We can introduce the implementing class as a abstract class and use the updating methods as abstract methods and implement them in the child classes that are extended from the abstract class. This is a simple solution for the violation of Open/Close Principle.</p><p>This creates a loose coupling structure within the software. Open/Close principle strongly focusing on Abstraction concept of OOP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*W8AXjDMmuyu84YNrZHiFaQ.png" /></figure><p><strong>Liskov Substitution Principle</strong></p><p>LSP states that “if S is a sub type of T, then objects of type T may be replaced with objects of type S” in simple words the ability to replace any instance of parent class with its child classes without any negative side effects. This is the main principle which explains us about the inheritance nature of the OOP concept.</p><p>If an instance of the subclass does something the super class doesn’t do then it’s a violation of LSP. If this seems to be a little hard let’s look into an example.</p><p>Let’s assume the “Square” class extends the “Rectangle’’ class. Square class always assumes the width is equal with the height. If a Square instance is used in a place where a Rectangle instance is needed unexpected behavior may occur. This is a typical example for violation of LSP. The common solution for this is using a Shape class, where both the Square and Rectangle class can extend and create their own methods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*oKOS1298SaUjoW7YNpwEnw.png" /></figure><p><strong>Interface Segregation Principle</strong></p><p>This principle states ‘’Client should not be forced to depend on interfaces they do not use ‘’. This basically says that <em>Don’t depend on things you don’t need.</em> This principle is mostly violated when the requirements of the software gets increased with time.</p><p>Let’s look into this problem in a practical way. Assume that you are developing a software for an ATM machine and your business client needs to add a message saying “This ATM will require an additional fee for withdrawal, do you Agree?” What will you do?</p><p>Simply you will add a method to the main interface and implement it. But this causes to recompile all the users of the interface and you need to redeploy the system. This violates the OCP.</p><p>The optimal solution for this will be to split the main interface to required number of interfaces according to the requirements and methods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*M8o2Vc0_ynd1RVf18rW7Rw.png" /><figcaption>one single interface for all methods.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/869/1*xbaNAXUiKOZL8Lg-b9k4gg.png" /><figcaption>interface split into two simple interfaces</figcaption></figure><p><strong>Dependency Inversion Principle</strong></p><p>This principle states that “High-level modules should not depend on Low-level modules. Both should depend on abstractions.” There is another part defined by this principle that is Abstraction should not depend on details. But details should depend on the abstractions. The main purpose of this principle is to reduce coupling between different pieces of code.</p><p>The main idea of this is to isolate our class behind the boundary formed by abstraction our class depends on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*ZbIubXg0VxRd2fVfU0d58w.png" /><figcaption>Tight coupled classes</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*CTQWFcXT3tSmbbO1x7o44A.png" /><figcaption>loose coupled classes</figcaption></figure><p>In the above diagram we can see that classes are communicating through interfaces rather than the concrete classes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=327def5b8a52" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>