This article is for anyone who has been using Java, with MVC background, e.g. Spring boot, and wants to upgrade his coding to use Spring WebFlux.
Most of the programming that is practiced falls under IMPERATIVE Programming. A simple example can be of lets say writing Fibonacci series. You end up assigning values to variables, then running them through some loop e.g. a for loop, and then using some logic to change its value. If you are developing a backend in Java, most of the time you are likely using the loops, variables, assigning them some values and then changing it based on logic.
In summary, IMPERATIVE programming is telling computer, HOW TO DO something
A typical example is below in Javascript language
| function fibonacciSeriesTillMaxNumber(max) { var fibArray=[] var a=1 var b=0 var c=a While (a < max) { c=a a=b+a b=c fibArray.push(a) } return fibArray } |
However, there is another kind of coding that is also popular, and it is known as DECLARATIVE programming. Here we just write what the result should look like, rather than telling how to obtain the result.
If you are a front-end engineer, you are probably using HTML & CSS. For example
| <img src="./image.jpg" / |
Here, you are just using declarative programming. You are just telling the browser to display an image without really bothering about how it is to be done.
Again, what about SQL programmers?
Let’s say you have a table like below
| Latitude | Longitude | Name |
| 38 | 122 | Berkeley |
| 42 | 71 | Cambridge |
| 45 | 93 | Minneapolis
|
And you run a query
| sqlite> select * from cities; |
This is an example again of declarative programming, where you are telling what is to be done, rather than bothering about how it is to be done.
And this is declarative programming v/s imperative programming i.e. “WHAT TO DO” v/s “HOW TO DO”
This can be thought of as a restricted way of DECLARATIVE programming, in which you are getting desired results by chaining two or more functions together.
This means that functions/methods can now be assigned to variables, added to objects and array. You can pass them as argument to function (chaining), they can be returned to function calls.
Another important thing is that now Data is immutable, so to change it, whether it’s a variable or object, you make copies of it.
And combining the above two concepts, you come up with Functional Programming, that is Functions change the data by returning new copies of data. In fact, for Functional programming to work, the functions must follow the rule of PURE function. A PURE function do not have side effects like network or database calls, the return value solely depends on the arguments it has.
Let’s try to understand Functional programming by an example.
Refer to the 3 functions below.
function fibonacciSeriesTillMaxNumber(max)
function sum(total, number) { return total+number; } |
Now, let’s try to write a functional program to find the sum of all the EVEN numbers in the fibonacci series till max value of 1000.
function main () } |
Explanation of above code: We begin by returning our fibonacci numbers, which we pass our maximum value of 1,000 to, that have been stored in an array. We then filter that array and pass our filter method with our isEven function. This updates the array to only return the fibonacci sequence numbers that are even. To perform our final task of summing those numbers together we pass our sum function to our reduce method.
The above is the power of functional programming -
Let’s say we are writing a simple Task Allocator. So there are two simple UI - the first UI is where the Manager allocates the tasks to his team, while the second UI is what different team members have. The Tasks are all stored in DB.
In a normal way of coding, let’s call it proactive, the team member”s UI will get updated whenever the DB has updated entry against his name.
Another way, that is reactive, is when
So that main difference here is that
Understanding OBSERVABLE
While writing REACTIVE code, there are additional challenges.
The OBSERVABLE is a
Webclient
Backpressure
This article is for anyone who has been using Java, with MVC background, e.g. Spring boot, and wants to upgrade his coding to use Spring WebFlux.
Share: