State & Logic : Building Scalable Applications - Part 1 - The Building Blocks
Code by Monky2020, CC BY-SA 4.0, via Wikimedia Commons

State & Logic : Building Scalable Applications - Part 1 - The Building Blocks

There are two building blocks (like Legos) when building applications in computer science. They are

  1. State
  2. Logic

State represents the data and logic represents how we expect to act on the data. Whether you write server side programs in languages like Java, C# or User Interfaces such as in React and JS or low level system programming in C, Rust or even assembly you will notice these two blocks is your programs.

To give an example, let say you click a photo in Instagram , apply filters and post it to your feed. The photo you clicked which is the data forms the state. When you apply filters this data / state is transformed via logic. When you post it to your feed, the mobile phone sends this transformed state to the Instagram server via logic and the server accepts the state and persists this state in their systems.

Similarly, what happens when you want to add two numbers? At the low level the numbers (state) are loaded into memory of the processors called registers. The add logic is executed by the processor on the state stored in these register and the result is a state which is stored back to the register.

So, the state takes memory and logic takes CPU cycles. How do high level languages allows us to use these two effectively?

There are two popular forms of programming styles.

a. Object Oriented Programming

b. Functional Programming

Object Oriented Programming

In Object Oriented Programming, we keep the state and the methods which act on this state together in a class.

Here is a very simple example code in C# which does addition of two numbers.

public class Adder {
	public int A { get; set; }
	public int B { get; set; }
	public int Result {get; private set; }
	public void Sum() {
		this.Result = this.A + this.B;
    }
}

Here variable A, B and Result represents the state of an object and Sum contains the logic which act on the state of the object. It sets the variable Result based on Value of A and B.

When the Sum method is invoked, it alters the state (Result). This is also known as side effect.

This snippet has below down sides

  1. The code is not explicit about the internal dependencies that variable A and B should be initialized before calling Sum
  2. Result can be accessed only after calling Sum method
  3. The result do not get invalidated when the value of A and B changes after calling the Sum
  4. The problem increases when this object is shared across multiple threads. We have to synchronize across threads because, we have shared the state.

As a developer, we should always strive for simplicity and ensure the above ambiguities are taken care. Functions solves this problem in a simple and elegant way.

Functional Programming

The same can be expressed as below C# snippet.

public int Sum(int a, int b) {
  return a+b;
}
  

In the code, there is state, but there is no shared state. Each time a method is called the system applies the logic and returns the result. As there is no other state change when this method is called. In other terms there is no side effect. Given the same input, the function always returns the same value this can be called as Pure functions.

The benefit of pure functions or stateless functions is that, they can be run parallelly in multiple cores, multiple systems and multiple data centers. The famous pattern "MapReduce" uses this fact to process big data in systems such as Hadoop.

Most of the Modern programming languages allows us to mix both Object Oriented and Functional styles of programming. As a developer, we have to pick and use the right tools for the right job.

Some of the key principle which can be considered based on your situation are

  1. Avoid shared state as much as you can.
  2. Prefer immutable state over mutable state to reduce ambiguity. Mutable Shared state will result is hard to debug problems.
  3. Write small methods which operate on small set of data and compose them together. The methods when read should be self explanatory.
  4. When using OOP style, especially for Dependency Injection, mark them read-only and design your classes to be immutable.
  5. When you want to build a scalable application which will not degrade as the number of users and data increases always consider keeping the state external to application. This will allow your app to run on multiple machines. This ability to run on multiple machines is called scaling out and is easier to do for logic compared to scaling state.

We can look at how cloud computing helps us to scale our applications and what can be considered for scaling state management in the next article in this series.

Views are my own. Not set in stone.

Raghavendra Sampathmurthy

Visionary IT & Data Analytics Leader with 20+ Years of Transformational Impact | Expert in Data Management & Advanced Analytics | Strategist in Banking & Supply Chain Digitalization

4 年

Good article Ramesh ??Looking forward for the series.

Ravi Kumar

Group SVP @ NTTDATA | Leading Offshore Teams, Driving Growth

4 年

Congratulations Ramesh Vijayaraghavan Very heartening to see this superior article ????

Abdul Kabir Sheik

Delivery Director at NTT DATA Services - Agile and Digital Transformation Leader | Product Management | Technology Enthusiast

4 年

Was wondering why this took so long from you to start tech blogging. Nice start. Look forward to the series Ramesh..

Mahesh Biradar

Software Developer

4 年

As a side note - what I saw from "Functional Programming" you can still have first calss functions and higher order functions which cannot be pure. So I assume you are talking about "Pure Functional Programming" rather than just Functional Programming!

回复
Pradeep Mahadeshwar

Having 11+ years of Experience as Cloud Certified (AWS & Azure) Full Stack Developer, AWS DevOps & Lead Engineer. E2E hands-on exposure with Microsoft .NET Technologies, Angular, & AWS Cloud.

4 年

Good read!

要查看或添加评论,请登录

Ramesh Vijayaraghavan ????的更多文章

社区洞察

其他会员也浏览了