Today we'll touch on functional programming. More specifically, we'll look at the difference between declarative and imperative programming.
First, let's quickly go over the terms. Then we'll compare these programming styles and see how they appear in Java and whether the language supports their happy coexistence.
Functional programming is a paradigm where functions are understood as mathematical functions, not as subroutines, as in procedural programming. That is, in these two paradigms interpret the word "function" differently. Remember this and don't confuse them. Java doesn't let you get confused, since subprograms are referred to as "methods", whereas functions refer to mathematical functions (also: lambda functions or method reference).
In practice, in procedural programming, functions depend not only on input variables, but also on external factors (such as other variables outside the function or the state of the system). That means that calling the same function with the same arguments but in a different context might produce different results. In functional programming, when a function is called with the same arguments, it always produces the same result, because functions depend only on input data.
Pros of functional programming
- Improved code reliability
- Convenient unit testing
- Opportunities for code optimization during compilation
- Opportunities for concurrency
Cons of functional programming
Functional programming's disadvantages stem from all these same features:
There are no assignment statements. Instead, new values are stored in new variables, leading to the need to constantly allocate and automatically release memory. As a result, highly efficiently garbage collection is an essential part of any system that executes functional programs.
Non-strict evaluation means the order of function calls is unpredictable, which creates I/O problems when the order of operations is important.
That concludes our quick review of functional programming. Now let's move on to programming styles.
Imperative programming is a programming paradigm characterized by the following features:
The program's source code consists of instructions (statements).
The instructions must be followed sequentially.
Data generated by executing previous instructions can be read from memory by subsequent instructions.
The data obtained by executing an instruction can be written to memory.
Here are the main features of imperative languages:
- Use of boolean variables.
- Use of the assignment operator.
- Use of compound expressions.
- Use of subroutines.
An imperative program is like orders expressed in the imperative mood in natural languages. In other words, an imperative program is a sequence of commands.
Imperative programming languages include C and C++.
Declarative programming is a programming paradigm in which the solution to a problem is specified. That is, the end result is described, not the way to achieve it. HTML is an example of a declarative language. When writing tags in this language, we don't think about how the elements will be drawn on the page. We just describe what the page should look like.
Another declarative programming language is SQL.
Let's compare the two styles of programming by considering a real life example: how do we explain to a person how to get to a certain place?
Imagine this situation: a man comes up to us on the street and asks, "How do I get to the museum?"
With an imperative approach, we would provide him with the algorithm of how to get there on foot:
- turn around right here
- walk 2 blocks in a straight line
- turn to the right
With a declarative approach, we would simply give the address, and then the person would get to the right place on his own.
Java is currently a multi-paradigm programming language. Multi-paradigm means that the language supports several paradigms.
During its long evolution, the language has extended its object-oriented model so that its users have different tools and can choose the best one for their specific task.
As a result, Java currently supports both the imperative approach (such as writing code for method calls) and the declarative approach (such as the annotations available at runtime).
There are various programming paradigms.
There are declarative and imperative approaches.
You should choose the one that is best suited for the task at hand.
Java is a multi-paradigm language that supports both approaches.
GO TO FULL VERSION