Imperative vs Declarative Languages
Imperative means giving an order or command. In imperative languages, we give orders to the computer and it performs the orders for us. Imperative programming focuses on describing how a program operates step by step.
Procedural programming is a type of imperative programming in which the program is built from one or more procedures (subroutines of functions). Object-oriented programming is another type of imperative programming but with a better structure to improve the maintainability of the programs.
In declarative programming, we define what the program must accomplish rather than tell it how to accomplish it as a sequence of commands. Two common declarative languages are database query languages like SQL and regular expressions. In a SQL command, we tell the program what we want and the query interpreter's role is to design and perform a process to produce the result. Another example is HTML markup language in which we define the content we want to see on the browser and it's up to the browser how to render them.
Functional programming is a type of declarative programming.
- Immutability: using
constfor defining variables.
- Higher-order Functions: passing functions as a parameter to another function and returning a function as a result of a function.
- Referential Transparency: each function only relies on its parameters for performing its job. These kinds of functions are predictable and they always return the same value for the same input no matter what the state of the system is.