Imperative vs declarative in Programming

wahyu eko hadi saputro
3 min readAug 7, 2022

Nowadays many paradigms in programming are coming up, and the most hype is declarative programming concepts. Imperative programming is programming where programmers will define the instruction / statements step by step while declarative programming is where the programming done with expressions or declarations instead of statements. One implementation of declarative programming is functional programming.

Functional programming, the word function comes from math function. example: A function from a set A to a set B is an assignment of an element of B to each element of A. The set A is called the domain of the function and the set B is called the codomain of the function.

In general function notation is :

But in more dept we can define the function with more precise definition :

and then become

Imperative programming:

Characteristic:

  • Programmer define the statement step by step
public int imperativeFibonaci(int fiboLength){
int fiboNumber = 0;
int a = 1;
int b = 1;
if (fiboLength == 0) {return 0;}
if (fiboLength == 1 || fiboLength == 2) {return 1;}
for (int i=3; i<=fiboLength; i++ ){
fiboNumber = a + b;
a = b;
b = fiboNumber;
}
return fiboNumber;
}

Functional Programming (FP)

Functional programming is a programming paradigm that models computation as the evaluation of expressions. And expressions are built using functions that don’t have mutable state and side effects. expression is not statement, on statement it has sequence of instruction, while an expression is function with no side effect and mutable state. side effect on here is change state inside function. State on here is like state of variable, the variable must be constant (no mutable variable).

Characteristic Functional Programming:

  • Pure function, like math function described above, no mutable state and side effects.
  • first-class functions and higher order function : that means, every function is an object like (integer object, string object) and an object can be assigned into a variable or passed as an argument to another function. higher order function is a function used as an argument to another function.
  • Variable inside function is immutable : once assign a variable with a value we can not change the value of the variable, so it lead to thread safe
  • Functions guarantee referential transparency : function with same input will be have same output. the function must be pure function and no side effect / no changing state / immutable.

Kind of side effect and mutable state inside function, which is prohibited in FP :

  • Modifying a variable
  • Modifying a data structure in place
  • Setting a field on an object
  • Throwing an exception or halting with an error
  • Printing to the console or reading user input
  • Reading from or writing to a file
  • Drawing on the screen

Piece of function in FP :

public  int functionalFibonaci(int n){
if(n == 0){
return 0;
}
if(n == 1 || n == 2){
return 1;
}
return functionalFibonaci(n-2) + functionalFibonaci(n-1);
}

More advance functional programming in java is implementation of lambda:https://wahyu-ehs.medium.com/java-lambda-a42d5e377464Example :

List lower = Arrays.asList("a","b","c");
System.out.println(lower.stream().map(s -> s.toString().toUpperCase()).collect(Collectors.toList()));
// outputs ["A", "B", "C"]

Source :

https://en.wikipedia.org/wiki/Function_(mathematics)

https://www.mathsisfun.com/sets/function.html

--

--