Kiebot Learning Solutions

Author name: Sujith PS (Co-founder & CTO)

Reactive Programming: Why should you care?

Reactive Programming: Why should you care? Reactive Programming is a way to build a scalable architecture that is resilient and quick to react to stimuli.Think of a spreadsheet. You have 3 cells A1, B1 and C1. You can configure an equation against C1 as the SUM(A1, B1). Here Cell C1 can respond to the changes of A1 and B1 without any further actions from the user. BLOGS Posted on September 9, 2021 By Sujith PS (Co-founder & CTO) September 9, 2021 What is Reactive Programming? Reactive Programming is a way to build a scalable architecture that is resilient and quick to react to stimuli. Think of a spreadsheet. You have 3 cells A1, B1 and C1. You can configure an equation against C1 as the SUM(A1, B1). Here Cell C1 can respond to the changes of A1 and B1 without any further actions from the user.Here we can say this spreadsheet is reactive towards the changes. The Cell C1 has been subscribed to the changes in cells A1 and B1, and does a simple addition over the updated values and displays the same.Isn’t this Cool? How can we use a similar strategy to programming? That’s reactive programming: changes propagate throughout a system automatically. Reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change. https://en.wikipedia.org/wiki/Reactive_programming In RP, Observables emit data and send it to the subscribers. In the above example, Cells A1 & B1 are Observables, and Cell C1 is a subscriber. Reactive representation of Spreadsheet Cells Why Reactive Programming? Reactive programming is about creating an architecture that supports :- Elasticity: The system stays responsive under varying workload. Message Driven: Communication between systems through messages. Responsive: The system responds in a timely manner if at all possible. Resilient: The system stays responsive in the face of failure. Use Cases Here are some of the examples where Reactive programming is used Facebook Spark AR Studio uses reactive programming to create relationships between objects, assets and values. Linkedin is using reactive principles to build online indicators for users on its social network. Verizon Wireless had used reactive principles to reduce response time to half in their eCommerce website. Netflix is a big believer in the Rx model because Rx has made it much easier for us to build complex asynchronous programs. Netflix Falkor provides developers with a unified model for interacting with both local and remote data, and it’s built on top of Rx. When to use Reactive Programming? Applications nowadays have an abundancy of real-time events of every kind that enable a highly interactive experience to the user. We need tools for properly dealing with that, and Reactive Programming is an answer. Retrieve data from a database and filter out data based on user settings/configurations. Render a UI that combines data from multiple data sources Create a realtime model of stock prices Log/display data from data sources such as wind, temperature or pressure sensors Highly Concurrent Message Consumers Highly depended on connected systems: Whenever there are so many depended on connected systems in which each of them are waiting for their successor to process some data and use that result. Evolution of Reactive Programming in Different languages Rx*library family is widely available for many languages and platforms Java :- https://github.com/ReactiveX/RxJava Ruby :- https://github.com/ReactiveX/RxRuby Python :- https://github.com/Reactive-Extensions/RxPy C++ :- https://github.com/ReactiveX/RxCpp NET :- https://github.com/dotnet/reactive Scala :- https://github.com/dotnet/reactive Swift :- https://github.com/dotnet/reactive Let’s look at Code Now StockServer Stock Server provides a stream of Stock info via the method getfeed . Observable Notice the Observable , Observable is simply a collection of data that waits to be invoked (subscribed) before it can emit any data.MainHere we are subscribing to the stocks feed on StockServer and prints the values and errors. Subscription Demo Repo Download demo code from :- https://github.com/sujithps/stock-exchange References Reactive Manifesto :- https://www.reactivemanifesto.org Reactive UI :- https://www.reactiveui.net/docs https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 https://blog.danlew.net/2017/07/27/an-introduction-to-functional-reactive-programming Recent Posts 24 May 2024 NestJS Authentication Deep Dive 04 Apr 2024 Node.js Development to the Next Level 26 Mar 2024 Mastering Agile Testing Social Linkedin Instagram Facebook Youtube Recent Posts _kiebot Follow on Instagram

Reactive Programming: Why should you care? Read More »

The Art of writing clean code

The Art of writing clean code It is every developer’s responsibility to write clean code. Code is clean when it is easy to read and understand by the team members. BLOGS Posted on November 11, 2020 By Sujith PS (Co-founder & CTO) November 11, 2020 It is every developer’s responsibility to write clean code. Code is clean when it is easy to read and understand by the team members. “Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. …[Therefore,] making it easy to read makes it easier to write.”― Robert C. MartinWhen writing a piece of code, the developer must understand that it’s not his/her code, it’s the team’s code. Those team members (current or future) are going to spend time with it. If the code is not readable, then the team is going to struggle. The Problem Why programmers are slow? It is very easy to start from scratch and implement new features to a new project. But, as the code base increases, the developers might take more time to implement new features.Why so? 🤔Developers 💻 normally do the following things while writing code:   Read the existing code and understand how it is working now (even if the code is written by the same developer). 📖 Try to find a way to solve the current problem. 🏗 Try to implement a solution, which won’t break existing features. ❎ As the number of lines in the existing code increases, it automatically adds up time in steps 1 and 3. Which will easily slow down the developers. ⏲ The Solution How to make the step1 faster? Write readable code. How to write readable code? There are several practices/principles to write readable code. 1. Take time to review the code One can assure writing readable code only by asking others to read it. Implement a review process by the colleagues so that each/some of the team members read other’s code and understand those. Make this process frequent. 2. Avoid unnecessary complexity Simple code communicates what it does clearly without exposing confusing/surprising details. One best way to make the code simple is by following YAGNI (You Ain’t Gonna Need It). At any point in time, before implementing something complex, ask whether it is really important to implement now or can it be delayed. How this help? 🤔. This depends on “how much do you know now” and “the cost of implementation”. If this is better to be implemented later with more detail then it is better to do so. YAGNI helps the developer to implement a simple and fast solution. 3. Avoid code duplications (DRY — Don’t Repeat Yourself) Extract the duplicate code and replace with higher-level abstractions. This will save time to read and understand the duplicate code again and again. 4. Focus on SRP (Single Responsibility Principle) Whenever writing functions or classes it often tend to implement multiple things into one abstraction. Which clearly adds complexity and increases the chance to write longer functions.Following SLAP (Single Level of Abstraction Principle) helps the developer to write short methods. SLAP Which says, each statement in a function should belong to the same level of abstraction. Which will help the readability, because it is cohesive and helps to extract non-cohesive code as other functions. 5. Write Tests This is one of the best ways to write readable code. Code is clean when it contains tests. We can think of tests in this way: tests are the explanations of our code. If you want to understand a particular logic in a piece of code, just look at the tests and which will explain why that logic is there. One can follow these steps to write good tests.  Recent Posts 24 May 2024 NestJS Authentication Deep Dive 04 Apr 2024 Node.js Development to the Next Level 26 Mar 2024 Mastering Agile Testing Social Linkedin Instagram Facebook Youtube Recent Posts _kiebot Follow on Instagram

The Art of writing clean code Read More »

Scroll to Top